I have been searching high and low but I can''t find an elegant way to map database column names to model names. The idea is simple if I have a column named ''CUSTOM_5'' I want this to show up as "favorite_pet''. So far all the suggestions I have seen are to create your custom methods which is fine but a lot of work if you have to map most if not all your fields. Also creating getters and setters means you now have to add those methods to the to_xml and to_json methods as well as excluding the original field names. Surely there is an easier way to handle this very common problem. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Could ruby''s ''alias'' command work for this? Something like: alias favorite_pet custom_5 alias favorite_pet= custom_5 Not sure where you''d put those lines though... -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Tim Uckun Sent: Thursday, July 17, 2008 5:06 PM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Is there an elegant way to map column names. I have been searching high and low but I can''t find an elegant way to map database column names to model names. The idea is simple if I have a column named ''CUSTOM_5'' I want this to show up as "favorite_pet''. So far all the suggestions I have seen are to create your custom methods which is fine but a lot of work if you have to map most if not all your fields. Also creating getters and setters means you now have to add those methods to the to_xml and to_json methods as well as excluding the original field names. Surely there is an easier way to handle this very common problem. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
That''s not gonna work because those methods don''t really exist. You could probably extend AR::Base''s method_missing method, but really, you should change the database columns'' names. On Jul 18, 12:52 pm, "Pardee, Roy" <parde...-go57ItdSaco@public.gmane.org> wrote:> Could ruby''s ''alias'' command work for this? Something like: > > alias favorite_pet custom_5 > alias favorite_pet= custom_5> > Not sure where you''d put those lines though... > > -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Tim Uckun > Sent: Thursday, July 17, 2008 5:06 PM > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > Subject: [Rails] Is there an elegant way to map column names. > > I have been searching high and low but I can''t find an elegant way to > map database column names to model names. > > The idea is simple if I have a column named ''CUSTOM_5'' I want this to > show up as "favorite_pet''. > > So far all the suggestions I have seen are to create your custom methods > which is fine but a lot of work if you have to map most if not all your > fields. > > Also creating getters and setters means you now have to add those > methods to the to_xml and to_json methods as well as excluding the > original field names. > > Surely there is an easier way to handle this very common problem.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
This should work. Usage: class GreenPastures < ActiveRecord::Base alias_column "new_name" => "old_nAmE" end Include this code in a file in /lib module Legacy def self.append_features(base) super base.extend(ClassMethods) end module ClassMethods def alias_column(options) options.each do |new_name, old_name| self.send(:define_method, new_name) { self.send(old_name) } self.send(:define_method, "#{new_name}=") { |value| self.send("#{old_name}=", value) } end end end end ActiveRecord::Base.class_eval do include Legacy end On Fri, Jul 18, 2008 at 1:18 PM, julian <thefool808-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > That''s not gonna work because those methods don''t really exist. You > could probably extend AR::Base''s method_missing method, but really, > you should change the database columns'' names. > > On Jul 18, 12:52 pm, "Pardee, Roy" <parde...-go57ItdSaco@public.gmane.org> wrote: > > Could ruby''s ''alias'' command work for this? Something like: > > > > alias favorite_pet custom_5 > > alias favorite_pet= custom_5> > > > Not sure where you''d put those lines though... > > > > -----Original Message----- > > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > > > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Tim Uckun > > Sent: Thursday, July 17, 2008 5:06 PM > > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > > Subject: [Rails] Is there an elegant way to map column names. > > > > I have been searching high and low but I can''t find an elegant way to > > map database column names to model names. > > > > The idea is simple if I have a column named ''CUSTOM_5'' I want this to > > show up as "favorite_pet''. > > > > So far all the suggestions I have seen are to create your custom methods > > which is fine but a lot of work if you have to map most if not all your > > fields. > > > > Also creating getters and setters means you now have to add those > > methods to the to_xml and to_json methods as well as excluding the > > original field names. > > > > Surely there is an easier way to handle this very common problem. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> > class GreenPastures < ActiveRecord::Base > > alias_column "new_name" => "old_nAmE" > > endI saw this during my search. I am wondering how this is different that simply using the ruby alias command. Of course this won''t effect the "to_xml" and "to_json" methods. They seem to ignore the local attr. I guess you''d have to build a hash, call this method for every member of the hash and finally pass those to the to_xml method. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---