# NoMethodError

**URL:** <https://www.ruby-forum.com/t/nomethoderror/130748>\
**Category:** Rails\
**Created:** [March 3, 2008, 12:30am UTC](https://www.ruby-forum.com/t/nomethoderror/130748 "2008-03-03T00:30:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [March 3, 2008, 12:30am UTC](https://www.ruby-forum.com/t/nomethoderror/130748/1 "2008-03-03T00:30:26Z")

</div>

Hi I’m having a NoMethodError

NoMethodError in Admin/updateController#index

undefined method `find’ for #\<Tag id: nil, name: “”, rank: nil,  
parent\_id: 0\>

in the model I have

def find\_subcategory(subcategory)

```
 subcategory_id = self.find(:all, :conditions => [ "subcategory

```

= ?", subcategory ])  
return subcategory\_id.id

end

in the controller I have

@tags = Tag.new  
@category\_id =  
@tags.find\_category(products[i].elements[“category”].text)

I don’t understand why the find method isn’t defined, isn’t find  
method predefined for every active record object. Any help greatly  
appreciated.

Thanks.

---

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [March 3, 2008, 4:41am UTC](https://www.ruby-forum.com/t/nomethoderror/130748/2 "2008-03-03T04:41:19Z")

</div>

The model looks like this

class Tag \< ActiveRecord::Base

has\_and\_belongs\_to\_many :products,  
:join\_table =\> ‘products\_tags’  
validates\_presence\_of :name  
validates\_uniqueness\_of :name  
acts\_as\_tree :order =\> ‘-rank DESC’

def find\_category(category)

# if tag for given category exists return the id, else create it and

return id  
category\_id = self.find(:all, :conditions =\> [ “category = ?”,  
category ])  
if category\_id then  
return category\_id.id  
else  
new\_category = self.new  
new\_category.name = category  
new\_category.save  
category\_id = self.find(:all, :conditions =\> [“category = ?”,  
category])  
return category\_id.id  
end

end

def find\_subcategory(subcategory)

```
 # if tag for given subcategory exists return the id, else create it

```

and return id

```
 subcategory_id = self.find(:all, :conditions => [ "subcategory

```

= ?", subcategory ])  
if subcategory\_id then  
return subcategory\_id.id  
else  
new\_subcategory = self.new  
new\_subcategory.name = subcategory  
new\_subcategory.save  
subcategory\_id = self.find(:all, :conditions =\> [“subcategory  
= ?”, subcategory])  
return subcategory\_id.id  
end  
end

end

products[i].elements[“category”].text

Comes from an xml file, that I’m importing, I’m doing a find to see if  
the category already exists in the database before I insert the  
product so that I can link the product with an appropriate category,  
and subcategory.

---

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [March 3, 2008, 4:28am UTC](https://www.ruby-forum.com/t/nomethoderror/130748/3 "2008-03-03T04:28:22Z")

</div>

By the looks of things, Tag is inherting correctly from  
ActiveRecord::Base,  
but could you show us your whole model anyway? There’s find\_category  
missing  
from what you gave us and I’m sure that:

products[i].elements[“category”].text

could be done much more nicer if I knew what you were trying to do! I  
think  
you’re iterating through the products, finding the category for each and  
then the text for that category and then doing a find on it. I don’t  
fully  
understand why you’re doing the find if you already know what the  
category  
is!

On Mon, Mar 3, 2008 at 9:59 AM, Dave [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> in the model I have
> 
> Thanks.
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [March 3, 2008, 10:38am UTC](https://www.ruby-forum.com/t/nomethoderror/130748/4 "2008-03-03T10:38:17Z")

</div>

On 3 Mar 2008, at 03:40, Dave wrote:

> acts\_as\_tree :order =\> ‘-rank DESC’
> 
> def find\_category(category)
> 
> # if tag for given category exists return the id, else create it and
> 
> return id  
> category\_id = self.find(:all, :conditions =\> [ “category = ?”,  
> category ])

find is a class method, not an instance method. from an instance  
method you have to say Tag.find or self.class.find.  
However, what I think you want to do here is make the find\_category  
method a class method.

Fred

---

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [March 4, 2008, 4:04am UTC](https://www.ruby-forum.com/t/nomethoderror/130748/5 "2008-03-04T04:04:04Z")

</div>

If you wanted to use “find\_category” as a method of Model “Tag”, then  
you must also define your method as such:

```
    def self.find_subcategory(subcategory)
            subcategory_id = self.find(:all, :conditions =>

```

[“subcategory= ?”, subcategory])  
return subcategory\_id.id  
end

I just put “self.” infront of the method name.

---

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [March 4, 2008, 6:26am UTC](https://www.ruby-forum.com/t/nomethoderror/130748/6 "2008-03-04T06:26:28Z")

</div>

Thanks for the help, I’ll see if I can get this working when I get a  
chance later on, however this does make sense.

---

<div class="post-metadata">

**Author:** ![dave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dave](https://www.ruby-forum.com/u/dave)\
**Post date:** [March 3, 2008, 10:59am UTC](https://www.ruby-forum.com/t/nomethoderror/130748/7 "2008-03-03T10:59:00Z")

</div>

Ahh good pick Fred!

On Mon, Mar 3, 2008 at 8:07 PM, Frederick C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > has\_and\_belongs\_to\_many :products,  
> > category\_id = self.find(:all, :conditions =\> [ “category = ?”,  
> > if category\_id then  
> > end  
> > if subcategory\_id then
> > 
> > >
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.
