On Dec 3, 2010, at 12:22 PM, Mike B wrote:

> I'm writing a Rails application against a legacy database. One of the tables 
> in this legacy database has a column named object_id. Unfortunately object_id 
> is also an attribute of every object in Ruby, so when ActiveRecord is trying 
> to use these objects to formulate a query it is using the Ruby defined 
> object_id, rather than the value that is in the database.
> 
> The legacy application is immense at well over a million lines of code, so 
> simply changing the name of the column in the database would be an option of 
> last resort.
> 
> Questions:
> 1. Is there any way to make ActiveRecord/Rails use an alias or synonym for 
> this column? (i.e. such that in my app I can access the value using the 
> alias, but when AR generates SQL it uses the actual column name)
> 2. Is there any way in Ruby to make the object_id method behave differently, 
> depending on who is calling it? (i.e. use database object_id when being 
> called by activerecord, otherwise return ruby object_id)
> 3. Can I simply override the behavior of the object_id method in my model (I 
> assume this is a terrible idea, but had to ask)
> 
> Any suggestions are greatly appreciated.

Ouch.  I don't have a definitive answer, but you maybe able to get away 
defining the following methods in your model...

def newobjid
  read_attribute(:object_id)
end

def newobjid=(val)
  write_atrribute(:object_id, val)
end

There may even be a short cut to the above...  definitely take a look at the 
source of read_attribute/write_attribute to see what they are doing and figure 
out where the short comings in the above are...

-philip

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to