Rails 3 Model Mapping Certain Columns To Different Model Attributes
I have the old legacy table called 'DXFTACCTS', and I created Rails model 'Account'. class Account < ActiveRecord::Base set_table_name 'DXFTACCTS' end The problem is that DXF
Solution 1:
You can use the method alias_attribute like this:
classAccount < ActiveRecord::Base
set_table_name "DXFTACCTS"
alias_attribute :first_name, :XORFNAMEendalias_attribute creates the methods first_name, first_name= and first_name? which will map to the XORFNAME column in your table. However, you will NOT be able to use it in conditions like regular columns. For example:
Account.all(:conditions => { :first_name =>"Foo" })
That will fail...
Solution 2:
I think something like definition of getter and setter methods should do the trick:
classAccount < ActiveRecord::Base
...
deffirts_nameself[:XORFNAME]
end
def first_name= value
self[:XORFNAME] = value
end
...
end
Post a Comment for "Rails 3 Model Mapping Certain Columns To Different Model Attributes"