# Help Adding "class method" to model

**URL:** <https://www.ruby-forum.com/t/help-adding-class-method-to-model/134421>\
**Category:** Rails\
**Created:** [April 10, 2008, 5:39pm UTC](https://www.ruby-forum.com/t/help-adding-class-method-to-model/134421 "2008-04-10T17:39:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![apswartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71e660/32.png) [@apswartz](https://www.ruby-forum.com/u/apswartz)\
**Post date:** [April 10, 2008, 5:39pm UTC](https://www.ruby-forum.com/t/help-adding-class-method-to-model/134421/1 "2008-04-10T17:39:35Z")

</div>

I can’t help but think this should be really easy, but I am pulling my  
hair out.

I have a model “Task”

class Task \< ActiveRecord::Base  
acts\_as\_nested\_set

def rootsForUser(user)  
…  
end  
end

In the controller, I have:  
@tasks = Task.rootsForUser(current\_user)

I get “NoMethodError in TasksController#index”

Why can’t I find it? (Is this some weird rails-ism where I haven’t  
named the method the “correct” thing?)

Thanks in advance,

Alan

---

<div class="post-metadata">

**Author:** ![apswartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71e660/32.png) [@apswartz](https://www.ruby-forum.com/u/apswartz)\
**Post date:** [April 10, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/help-adding-class-method-to-model/134421/2 "2008-04-10T17:42:32Z")

</div>

First of all, I _highly_ suggest you stop your Rails stuff and learn  
Ruby itself:

[http://whytheluckystiff.net/ruby/pickaxe/](http://whytheluckystiff.net/ruby/pickaxe/)

That said, here’s the short of what you’re doing.

In your code, you’re defining an instance method for Task. Thus t =  
Task.new; t.rootsForUser(user) is what you’ve told Ruby you want.

If you want a class method, it’s simply:

class Task \< ActiveRecord::Base  
acts\_as\_nested\_set

def self.rootsForUser(user)  
…  
end  
end

The reasons for the “self” are explained in the link above. Please read  
it.

Jason R.

---

<div class="post-metadata">

**Author:** ![apswartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71e660/32.png) [@apswartz](https://www.ruby-forum.com/u/apswartz)\
**Post date:** [April 10, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/help-adding-class-method-to-model/134421/3 "2008-04-10T17:43:07Z")

</div>

> Why can’t I find it? (Is this some weird rails-ism where I haven’t  
> named the method the “correct” thing?)
> 
> Thanks in advance,
> 
> Alan

Hi Alan,

Does renaming your method to self.rootsForUser(user) help?

Robin

---

<div class="post-metadata">

**Author:** ![apswartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71e660/32.png) [@apswartz](https://www.ruby-forum.com/u/apswartz)\
**Post date:** [April 10, 2008, 5:48pm UTC](https://www.ruby-forum.com/t/help-adding-class-method-to-model/134421/4 "2008-04-10T17:48:25Z")

</div>

On 10 Apr 2008, at 16:44, Alan S. wrote:

> No, renaming it to self.rootsForUser(user) does not help. I get the  
> same error.

Well you do need to declare it as  
def self.rootsForUser(user)  
…  
end  
that’s just the way ruby works. On top of that there may be something  
else wrong (the error message in the log files should say exactly  
which method couldn’t be found for example.

Fred

---

<div class="post-metadata">

**Author:** ![apswartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71e660/32.png) [@apswartz](https://www.ruby-forum.com/u/apswartz)\
**Post date:** [April 10, 2008, 5:44pm UTC](https://www.ruby-forum.com/t/help-adding-class-method-to-model/134421/5 "2008-04-10T17:44:54Z")

</div>

No, renaming it to self.rootsForUser(user) does not help. I get the  
same error.

–Alan

---

<div class="post-metadata">

**Author:** ![apswartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71e660/32.png) [@apswartz](https://www.ruby-forum.com/u/apswartz)\
**Post date:** [April 10, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/help-adding-class-method-to-model/134421/6 "2008-04-10T17:53:11Z")

</div>

I made a mistake. It had gone on to my next problem. Which I now  
have resolved. Thanks for your help. I have read the pickaxe book,  
but for it to become working knowledge in my head I have to have done  
a project, which I am doing now. Perhaps I should have done something  
less ambitious, but I am learning a lot. 🙂

Thanks,

Alan

On Thu, Apr 10, 2008 at 10:47 AM, Frederick C.
