Debug help -- simple method produces NoMethodError

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?

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.

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.

On Jan 20, 7:28 pm, Jason P. [email protected]