I have a legacy database with a column named ''class'' which is causing Rails problems. Renaming the column isn''t an option and the workarounds I''ve seen so far don''t suit. I''m running rails 2.0.2 and an example of the error thrown looks like this... undefined method `generated_methods'' for "ADD":String c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/attribute_methods.rb:188:in `method_missing'' (The value returned in the ''class'' column is ''ADD'' in this case. Can anyone suggest a nice, neat, dry, satisfying solution? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jan-11 10:06 UTC
Re: ActiveRecord column with reserved name ''class''
On 11 Jan 2008, at 09:40, Derek Mailer wrote:> > I have a legacy database with a column named ''class'' which is causing > Rails problems. Renaming the column isn''t an option and the > workarounds > I''ve seen so far don''t suit. > > I''m running rails 2.0.2 and an example of the error thrown looks like > this... > undefined method `generated_methods'' for "ADD":String > > c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/ > attribute_methods.rb:188:in > `method_missing'' >How about class MyTable < AR:Base class << self def instance_method_already_implemented?(method_name) return true if method_name == ''class'' super end end end If I''m reading things right, that will stop AR from generating a class method to read your attribute. You can still get at it via foo[:class] (or define you own method returning read_attribute(:class) Fred> (The value returned in the ''class'' column is ''ADD'' in this case. > > Can anyone suggest a nice, neat, dry, satisfying solution? > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> How about > class MyTable < AR:Base > class << self > def instance_method_already_implemented?(method_name) > return true if method_name == ''class'' > super > end > end > end > > If I''m reading things right, that will stop AR from generating a class > method to read your attribute. You can still get at it via foo[:class] > (or define you own method returning read_attribute(:class)You''re absolutely right and that''ll ding dang do for me very nicely thanks, Fred! I no longer have errors in all the places they were showing before. Just as a note to anyone else reading this post, you might need to restart script/console rather than issue a reload! command after implementing this fix. Derek -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Worked great for me as well. Thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---