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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 <digitalm-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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. > > > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 3 Mar 2008, at 03:40, Dave wrote:> > > 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> > 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. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ahh good pick Fred! On Mon, Mar 3, 2008 at 8:07 PM, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On 3 Mar 2008, at 03:40, Dave wrote: > > > > > > > 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 > > > > > 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. > > > > > > > > > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Mar 3, 11:40 am, Dave <digit...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Mar 3, 8:03 pm, Chris <maricris.non...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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. > > On Mar 3, 11:40 am, Dave <digit...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---