# Debug help -- simple method produces NoMethodError

**URL:** <https://www.ruby-forum.com/t/debug-help-simple-method-produces-nomethoderror/82774>\
**Category:** Rails\
**Created:** [January 20, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/debug-help-simple-method-produces-nomethoderror/82774 "2007-01-20T19:15:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [January 20, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/debug-help-simple-method-produces-nomethoderror/82774/1 "2007-01-20T19:15:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [January 20, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/debug-help-simple-method-produces-nomethoderror/82774/2 "2007-01-20T19:28:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [January 20, 2007, 8:28pm UTC](https://www.ruby-forum.com/t/debug-help-simple-method-produces-nomethoderror/82774/3 "2007-01-20T20:28:17Z")

</div>

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.

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

On Jan 20, 7:28 pm, Jason P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
