# Having trouble with #method() method

**URL:** <https://www.ruby-forum.com/t/having-trouble-with-method-method/88135>\
**Category:** Rails\
**Created:** [March 8, 2007, 1:10pm UTC](https://www.ruby-forum.com/t/having-trouble-with-method-method/88135 "2007-03-08T13:10:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [March 8, 2007, 1:10pm UTC](https://www.ruby-forum.com/t/having-trouble-with-method-method/88135/1 "2007-03-08T13:10:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [March 8, 2007, 2:47pm UTC](https://www.ruby-forum.com/t/having-trouble-with-method-method/88135/2 "2007-03-08T14:47:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [March 8, 2007, 3:49pm UTC](https://www.ruby-forum.com/t/having-trouble-with-method-method/88135/3 "2007-03-08T15:49:11Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [March 8, 2007, 10:07pm UTC](https://www.ruby-forum.com/t/having-trouble-with-method-method/88135/4 "2007-03-08T22:07:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [March 9, 2007, 2:36am UTC](https://www.ruby-forum.com/t/having-trouble-with-method-method/88135/5 "2007-03-09T02:36:46Z")

</div>

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

On Mar 9, 5:07 am, Chad T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
