NoMethodError

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.

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!

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.

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 ])

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

Ahh good pick Fred!

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.

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.