On Mon, May 10, 2010 at 7:37 PM, David Zhu <dzwestwindso...@gmail.com> wrote:
> Thanks guys,
>
> If I have nested associations, how could i count that? For ex-
>
> I want to do something like @totalcount = Post.comment.count , or
> something like that
>
> How could i do that? ( i want to count the comments that belong to
> that post)

Assuming that Post has_many :comments, and you've gotten the
particular post of interest to be referenced by the variable post.

For the sake of the example below, let's say that the id of that post is 42

then either

post.comments.count

or

post.comments.size

will do a sql query like

  SELECT count(*) AS count_all FROM `users` WHERE (`users`.account_id = 42)

and return the value of count_all

HTH
-- 
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

-- 
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-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to