I am having trouble with the use of method() method. I am successfully creating a new ActiveRecord object for a model I have called Person: new_foreign_object = case foreign_class when ''Person'' then find_or_create_person(grandchild.content) else nil end if new_foreign_object p new_foreign_object p new_foreign_object.method("#{foreign_field}=") end The first ''p'' prints out the Person object just fine but the second ''p'' shows this error: ...:in `method'': undefined method `full_name='' for class `Person'' (NameError) I am very confused by this behavior as I would have expected that it should be looking through the instance of Person not the ''class'' as it says. The method definitely exists as I have called it manually and it works. I would appreciate it if someone could point out what I am doing wrong or what I have missed about the functionality of the method() method. 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 -~----------~----~----~----~------~----~------~--~---
Chad Thatcher wrote:> > > I am having trouble with the use of method() method. I am successfully > creating a new ActiveRecord object for a model I have called Person: > > > new_foreign_object = case foreign_class > when ''Person'' then > find_or_create_person(grandchild.content) > else nil > end > > if new_foreign_object > p new_foreign_object > p new_foreign_object.method("#{foreign_field}=") > end > > > The first ''p'' prints out the Person object just fine but the second ''p'' > shows this error: > > ...:in `method'': undefined method `full_name='' for class `Person'' > (NameError) > > I am very confused by this behavior as I would have expected that it > should be looking through the instance of Person not the ''class'' as it > says. The method definitely exists as I have called it manually and it > works. > > I would appreciate it if someone could point out what I am doing wrong > or what I have missed about the functionality of the method() method. > Thanks.I don''t know anything about the ''method()'' method, but you can achieve what you''re trying to do by doing... To get the value of the foreign field: new_foreign_object.send(foreign_field) To SET the value of the foreign field: new_foreign_object.send("#{foreign_field}=", some_value) Hope this helps, Dave. -- 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 -~----------~----~----~----~------~----~------~--~---
ActiveRecord doesn''t actually create methods for every attribute in your model. Instead it catches any missing methods by overriding the missing_method() method on ActiveRecord::Base and passing it to write_attribute(). If you want a list of attributes try:>> new_foreign_object.attributes.keys...>> # or?> Person.columns.map(&:name) ... On Mar 8, 8:10 pm, Chad Thatcher <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I am having trouble with the use of method() method. I am successfully > creating a new ActiveRecord object for a model I have called Person: > > new_foreign_object = case foreign_class > when ''Person'' then > find_or_create_person(grandchild.content) > else nil > end > > if new_foreign_object > p new_foreign_object > p new_foreign_object.method("#{foreign_field}=") > end > > The first ''p'' prints out the Person object just fine but the second ''p'' > shows this error: > > ...:in `method'': undefined method `full_name='' for class `Person'' > (NameError) > > I am very confused by this behavior as I would have expected that it > should be looking through the instance of Person not the ''class'' as it > says. The method definitely exists as I have called it manually and it > works. > > I would appreciate it if someone could point out what I am doing wrong > or what I have missed about the functionality of the method() method. > Thanks. > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Thanks guys I''ll have a try of send() the attributes() properties. I guess this also explains why I can''t get hold of the dynamic finders this way as well seeing as they are also probably catered for dynamically. All the best, Chad. -- 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 -~----------~----~----~----~------~----~------~--~---
Yup. ActiveRecord::Base has a method_missing defined on the class level which implement these finders... On Mar 9, 5:07 am, Chad Thatcher <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Thanks guys I''ll have a try of send() the attributes() properties. I > guess this also explains why I can''t get hold of the dynamic finders > this way as well seeing as they are also probably catered for > dynamically. > > All the best, > > Chad. > > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---