I have a legacy MySQL database with a weird naming convention - all table and column names have a prefix ''_'' , which is annoying, and some of the column names are misnomers. I would like to apply a pre-defined mapping between MySQL column names and method names to be used in the Ruby class, without having the original column names being visible anymore. how can I pre-define a static mapping of all (legacy) column names to different Class method names? e.g.: given legacy schema: Table _fix has columns: _job _ change _date _effect When defining the Ruby class, I''d like to do something like this: class Fix < ActiveRecord::Base self.table_name = "_fix" use_mapping ( # imaginary construct.. :_job => "job_id", :_change => "change_id" , :_date => "date" , :_effect => "status") self.primary_key = "job_id" end and want to access it like this: Fix.find( id ).status = "partial fix" which should acces the Is there a way to do this? thanks Tilo --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Apr 27, 2007, at 1:22 AM, tilo wrote:> class Fix < ActiveRecord::Base> self.table_name = "_fix" > use_mapping ( # imaginary construct.. > :_job => "job_id", > :_change => "change_id" , > :_date => "date" , > :_effect => "status")You could add this somewhere, for instance in environment.rb: class ActiveRecord::Base def self.map_column(legacy, nice) class_eval <<-EOS def #{nice} #{legacy} end def #{nice}=(val) self.#{legacy} = val end EOS end end The usage is class Model < ActiveRecord::Base map_column :_job, :job_id map_column :_change, :change_id ... end You see the technique in case you prefer another kind of macro. BTW, that cannot be based on alias_method because AR accessors are not defined by then. -- fxn --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---