Hi, I am trying to invoke a method on an ActiveRecord object using something like: k = :dateobj.method(k).call(new_date) Initially obj.method(k) is throwing a NameError. However, if I first explicitly invoke _any_ accessor method, then the above works as expected: obj.date # just get the date k = :dateobj.method(k).call(new_date) # works now Is there a good way to ensure that these accessor methods are loaded before I try looking them up with .method()? Thanks, Erik -- 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 -~----------~----~----~----~------~----~------~--~---
> Is there a good way to ensure that these accessor methods are loaded > before > I try looking them up with .method()?obj.public_methods.include(''k'') ? (Also I can''t remember if k must be a symbol - :k) BTW I doubt your premise, because I thought all attribute accessors get generated at .find time... --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
David A. Black
2008-Jul-20 01:40 UTC
Re: looking up accessor methods for ActiveRecord objects
Hi -- On Sun, 20 Jul 2008, Erik Rantapaa wrote:> > Hi, > > I am trying to invoke a method on an ActiveRecord object using something > like: > > k = :date> obj.method(k).call(new_date) > > Initially obj.method(k) is throwing a NameError. However, if I first > explicitly invoke _any_ accessor method, then the above works as > expected: > > obj.date # just get the date > k = :date> obj.method(k).call(new_date) # works nowInterestingly, that will happen when you call any unknown method on the object:>> t=> #<Team id: 277401923, name: "Persuaders", mascot: "Cat", ... >>> t.methods.grep(/mascot/)=> []>> t.blahNoMethodError: undefined method `blah'' for #<Team:0x242e2d8>>> t.methods.grep(/mascot/)=> ["mascot", "mascot=", "mascot?"] method_missing creates the column attributes immediately if they don''t exist, no matter what. The same thing happens with respond_to?> Is there a good way to ensure that these accessor methods are loaded > before > I try looking them up with .method()?You could call define_attribute_methods on the class. That''s basically what method_missing does. David -- Rails training from David A. Black and Ruby Power and Light: Intro to Ruby on Rails July 21-24 Edison, NJ Advancing With Rails August 18-21 Edison, NJ See http://www.rubypal.com for details and updates! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
David A. Black
2008-Jul-20 01:42 UTC
Re: looking up accessor methods for ActiveRecord objects
Hi -- On Sat, 19 Jul 2008, Phlip wrote:> >> Is there a good way to ensure that these accessor methods are loaded >> before >> I try looking them up with .method()? > > obj.public_methods.include(''k'')The column attributes aren''t there initially. (Try it; you''ll see.)> ? (Also I can''t remember if k must be a symbol - :k) > > BTW I doubt your premise, because I thought all attribute accessors get > generated at .find time...No, find doesn''t define the column attribute methods. David -- Rails training from David A. Black and Ruby Power and Light: Intro to Ruby on Rails July 21-24 Edison, NJ Advancing With Rails August 18-21 Edison, NJ See http://www.rubypal.com for details and updates! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---