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