Having trouble with #method() method

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 methodfull_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.

Chad T. 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 methodfull_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.

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 T. [email protected]

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.

Yup. ActiveRecord::Base has a method_missing defined on the class
level which implement these finders…

On Mar 9, 5:07 am, Chad T. [email protected]