I''ll get right to it and post some code: Site_controller < AC has_many :projects def delete_projects if Project.delete_all_for_site(params[:id]) #do some stuff here end end end class Project < ARB belongs_to :site def delete_all_for_site(site) destroy_all("site_id = ?", site) end end This produces a no method error: NoMethodError (undefined method `delete_all_for_site'' for Project:Class) What? delete_for_site is clearly a Project method. What gives? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
David wrote:> I''ll get right to it and post some code: > > Site_controller < AC > has_many :projects > > def delete_projects > if Project.delete_all_for_site(params[:id]) > #do some stuff here > end > end > > end > > class Project < ARB > belongs_to :site > > def delete_all_for_site(site) > destroy_all("site_id = ?", site) > end > end > > This produces a no method error: > > NoMethodError (undefined method `delete_all_for_site'' for Project:Class) > > What? delete_for_site is clearly a Project method. What gives?I see two problems? Is your first bit of code supposed to be a controller or a model? The second, and I think the answer you''re looking for is that you are calling an instance method, ''delete_all_for_sites'' on the class Project rather than an instance of Project. To define a class method instead try def self.delete_all_for_site(site) ... end But really, you probably want to use that as an instance method and refer to self.sites instead. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
augustlilleaas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jan-20 19:14 UTC
Re: Debug help -- simple method produces NoMethodError
It is clearly NOT a Project class method. def method end that''s a instance method. Those are written as Project#method. In other words, an instance of project, such as @project = Project.find(:first), then @project.method. def self.method end (writing self is the equivalent of writing Project.method) This is a class method, and you access it by going Project.method. The method and self.method are two different methods, and can co-exist even though they are named the "same". Also, you are obviously missing some basic rudy knowledge, as you say that that first block is a controller (it is obviously a model). Or perhaps you were drunk/tired? =P I strongly reccomend the Ruby for Rails book, it really revolutioned my way of using rails. http://www.manning.com/black/ On Jan 20, 7:28 pm, Jason Perry <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> David wrote: > > I''ll get right to it and post some code: > > > Site_controller < AC > > has_many :projects > > > def delete_projects > > if Project.delete_all_for_site(params[:id]) > > #do some stuff here > > end > > end > > > end > > > class Project < ARB > > belongs_to :site > > > def delete_all_for_site(site) > > destroy_all("site_id = ?", site) > > end > > end > > > This produces a no method error: > > > NoMethodError (undefined method `delete_all_for_site'' for Project:Class) > > > What? delete_for_site is clearly a Project method. What gives?I see two problems? Is your first bit of code supposed to be a > controller or a model? > > The second, and I think the answer you''re looking for is that you are > calling an instance method, ''delete_all_for_sites'' on the class Project > rather than an instance of Project. > > To define a class method instead try > > def self.delete_all_for_site(site) > ... > end > > But really, you probably want to use that as an instance method and > refer to self.sites instead. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---