# Model methods - is this the best way?

**URL:** <https://www.ruby-forum.com/t/model-methods-is-this-the-best-way/123845>\
**Category:** Rails\
**Created:** [December 21, 2007, 7:47am UTC](https://www.ruby-forum.com/t/model-methods-is-this-the-best-way/123845 "2007-12-21T07:47:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [December 21, 2007, 7:47am UTC](https://www.ruby-forum.com/t/model-methods-is-this-the-best-way/123845/1 "2007-12-21T07:47:28Z")

</div>

Hey,

I am putting quite alot of methods in my models such as User, for  
things such as:

class User \< ActiveRecord::Base  
…

# Count all new messagebox messages

def count\_new\_mbox  
count\_new\_privmsgs + count\_new\_comments + count\_new\_requests  
end

# Count new private messages

def count\_new\_privmsgs  
privmsgs.find(:all, :conditions =\> “is\_unread = 1”).size  
end

# Count number of new comments for the user

def count\_new\_comments  
user\_comments.find(:all, :conditions =\> “is\_unread = 1”).size  
end  
end

is this the best way to go about implementing these methods? cos it  
seems to me that im going to have HEAPS of these methods in my model,  
for even my finds. eg. find\_recent\_comments(n),  
find\_recent\_privmsgs(n).

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [December 21, 2007, 7:52am UTC](https://www.ruby-forum.com/t/model-methods-is-this-the-best-way/123845/2 "2007-12-21T07:52:44Z")

</div>

You could do a :counter\_cache on the has\_many relationship. This way it  
stores it as a field on your user record rather than working it out on  
the  
fly whenever it’s required.

has\_many :unread\_messages, :class\_name =\> “Message”, :conditions =\>  
“unread  
= 1”, :counter\_cache =\> true

On Dec 21, 2007 5:16 PM, Chubbs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def count\_new\_mbox  
> user\_comments.find(:all, :conditions =\> “is\_unread = 1”).size  
> end  
> end
> 
> is this the best way to go about implementing these methods? cos it  
> seems to me that im going to have HEAPS of these methods in my model,  
> for even my finds. eg. find\_recent\_comments(n),  
> find\_recent\_privmsgs(n).
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [December 21, 2007, 7:57am UTC](https://www.ruby-forum.com/t/model-methods-is-this-the-best-way/123845/3 "2007-12-21T07:57:24Z")

</div>

ahh wow, thanks didnt know about that. so will that return an array  
of Message objects ?

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [December 21, 2007, 9:32am UTC](https://www.ruby-forum.com/t/model-methods-is-this-the-best-way/123845/4 "2007-12-21T09:32:52Z")

</div>

Consider good oo programming concepts while coding. The methods  
related to messages should go into message model.

It is unfortunate even plugins like restful authentication violate  
basics of oo programming. It becomes even more difficult for newbies  
to write good code.

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [December 21, 2007, 7:58am UTC](https://www.ruby-forum.com/t/model-methods-is-this-the-best-way/123845/5 "2007-12-21T07:58:34Z")

</div>

Yes, as only has\_many does.

On Dec 21, 2007 5:27 PM, Chubbs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Hey,  
> > > end  
> > > end
> > 
> > > is this the best way to go about implementing these methods? cos it  
> > > seems to me that im going to have HEAPS of these methods in my model,  
> > > for even my finds. eg. find\_recent\_comments(n),  
> > > find\_recent\_privmsgs(n).
> > 
> > –  
> > Ryan B.[http://www.frozenplague.net](http://www.frozenplague.net)

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [December 21, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/model-methods-is-this-the-best-way/123845/6 "2007-12-21T13:13:14Z")

</div>

On Dec 21, 2007 10:44 AM, Chubbs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So you mean to put inside message something like :
> 
> class Message \< ActiveRecord::Base  
> def count\_unread(user)  
> user.messages.find(:all, :conditions =\> “is\_unread ==  
> true”).size;  
> end  
> end

I’m a bit new to Rails, but doesn’t scoping let you do something like

user.messages.count\_unread

(Assuming user has\_many messages and message belongs\_to user.)  
Then implement a class method similar to your method above.

class Message \< ActiveRecord:Base  
belongs\_to =\> :user  
def self.count\_unread  
find(:all, :conditions =\> {:is\_unread =\> true}).count  
end  
end

AFAIK, all finds on user.messages are scoped as if you had a condition  
:user\_id =\> user.id, but I haven’t looked at the details. Maybe  
someone else can confirm or correct me.

- Martin

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [December 21, 2007, 10:45am UTC](https://www.ruby-forum.com/t/model-methods-is-this-the-best-way/123845/7 "2007-12-21T10:45:29Z")

</div>

To me it makes more sense to put all the methods relating a User to  
fetch and retrieve data inside the User.

Why would it be good oo programming to put these methods inside my  
message object?

So you mean to put inside message something like :

class Message \< ActiveRecord::Base  
def count\_unread(user)  
user.messages.find(:all, :conditions =\> “is\_unread ==  
true”).size;  
end  
end

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [December 21, 2007, 2:56pm UTC](https://www.ruby-forum.com/t/model-methods-is-this-the-best-way/123845/8 "2007-12-21T14:56:24Z")

</div>

ok thanks so much guys for your help, ill defiantly go about  
implementing my methods like that from now on,

thanks,

chubbs

---

<div class="post-metadata">

**Author:** ![Chubbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@Chubbs](https://www.ruby-forum.com/u/Chubbs)\
**Post date:** [December 21, 2007, 1:32pm UTC](https://www.ruby-forum.com/t/model-methods-is-this-the-best-way/123845/9 "2007-12-21T13:32:34Z")

</div>

On 21 Dec 2007, at 13:12, Martin S. wrote:

> AFAIK, all finds on user.messages are scoped as if you had a condition  
> :user\_id =\> user.id, but I haven’t looked at the details. Maybe  
> someone else can confirm or correct me.

Correct, also, the use of .size is a very bad solution, as it will  
effectively load all the records into memory (as an array of  
records). The count method will perform an SQL count.

Best regards

Peter De Berdt
