views:

205

answers:

2

Hi All,

I'm having a slight issue with one of my Rails models. It's nothing I can't work around (as I've already done so), but I'm interested in the actual issue.

Basically, I have an ActiveRecord-based class named "Events" that I've connected to a remote database using "establish_connection". It all works fine and I can even perform an "Event.find(:all)". However, I am unable to use any of the convenience (column_name) methods. I am forced to do something like the following:

for each event in Event.find(:all)
  puts event["Point"]
  puts event["Timestamp"]
end

...instead of

for each event in Event.find(:all)
   puts event.point
   puts event.timestamp
end

FYI, when I generated the model I only ran "script/generate model event" and absolutely nothing else. I then used "establish_connection" and "set_table" inside the model in order to get a connection. Besides that I've done nothing. Again, querying works, but I don't get my convenience access.

Ideas?

Best.

+2  A: 

It looks like you have a capitalization issue - since your columns are capitalized, ActiveRecord will create capitalized attributes for you. Your second example may work correctly if you do event.Point instead of event.point. If you prefer using the lowercase versions, you may be able to use alias_attribute, from ActiveSupport:

alias_attribute :point, :Point
alias_attribute :timestamp, :Timestamp

...etc.

Greg Campbell
Wow, I can't believe I missed that. That was *exactly* the issue. You guys rock!Best.
humble_coder
A: 

Apart from the alias_attribute

If you have access to that application that controls that remote db and can make changes.

You can use ActiveResource, which is very very clean way for accessing something like a Remote Model.

http://railscasts.com/episodes/94-activeresource-basics

http://railscasts.com/episodes/95-more-on-activeresource

Rishav Rastogi