Find ALL and then count them up?

Is it possible to do a find all, and then return the # of items?

David Zhu wrote:

Is it possible to do a find all, and then return the # of items?

Better to use the built-in aggregate functions.

http://railsapi.com/doc/rails-v2.3.5/classes/ActiveRecord/Calculations/ClassMethods.html#M001357

This will perform the count in SQL and will be much more efficient than building all those ActiveRecord objects.

+1, but if you already have them you can use .size() to get the count.

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)

thanks

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

Thank you,

I swear this is the last question about this-

Is there a way to go one level deeper, so instead of @totalcount = Post.comments.count, could I have lets say... ---

@totalcount = Post.comments.davids.count, if comments has many davids?

So basically instead of just post has many comments, i also want to add comments has many davids, and then find out how many davids are in the Post.

So whats the syntax for that?

DOes that make sense? How could I do something like that? Thanks :slight_smile:

Thank you,

I swear this is the last question about this-

Is there a way to go one level deeper, so instead of @totalcount = Post.comments.count, could I have lets say... ---

@totalcount = Post.comments.davids.count, if comments has many davids?

a Comment might have many davids, but you have comments.

So basically instead of just post has many comments, i also want to add comments has many davids, and then find out how many davids are in the Post.

so for each comment, you want the davids, and count up the total

So whats the syntax for that?

DOes that make sense? How could I do something like that? Thanks :slight_smile:

@davidcount = Post.comments.map {|comment| comment.davids.count}.sum

But it might be more efficient to say something like:

@davidcount = David.count

depending on what you really want to know.

-Rob

Rob Biedenharn http://agileconsultingllc.com   Rob@AgileConsultingLLC.com

  rab@GaslightSoftware.com

Oh ok thanks!

But just to make sure we are on the same page-

Page has many comments Comments has many Davids

Would

@davidcount = Post.comments.map {|comment| comment.davids.count}.sum

find out how many David's are in a certain Post?

Just to clarify, im not trying to find All of the David's that exsit in the entire database, just the amount of Davids that belongs to the post. But the part i dont understand is how to count up the David's that belongs to that Post, because there are comments in between. (post has many comments, comments has many davids) And i want to find out for a certain post (@post) how many davds belongs to it)

So would @davidcount = Post.comments.map {|comment| comment.davids.count}.sum work for something like that?

Thank you

Oh ok thanks!

But just to make sure we are on the same page-

Page has many comments Comments has many Davids

I believe that if you also say Page has_many davids through comments then you can get at all the davids for a post by @post.davids and hence you can use @posts.davids.count

Colin

i need a join table? is the join table my comments table?

Yes but..

First I notice that you continue to use code of the form

   Post.comments. ....

Which won't work, since the associations like belongs_to, has_many ... create INSTANCE rather than class methods.

You need to use something like

  some_post.comments. ..

where some_post refers to a particular instance of Post.

Second, I suspect that David here is a hypothetical model class, and that what you might really be trying to do is count all the comments made by a particular user. Maybe you want something like:

class Post < ActiveRecord::Base     has_many :comments end

class User < ActiveRecord::Base end

class Comment < ActiveRecord::Base    belongs_to :post    belongs_to : user    named_scope :by_user, lambda {|user| :conditions => {:user => user} end

then

some_post = Post.find(params[:id]) # or some other code to get a particular post david = User.find_by_userid("david") # or some other code to get a particular user

some_post.comments.by_user(david).count

i need a join table? is the join table my comments table?

It already is a join table, you just need to tell rails that it can use it as such.

By the way could you not top post (so insert your comments at the appropriate point in the email you are replying to instead, as I have done here)? It makes it easier to follow the thread. For example your mail just says "I need a join table? .." but it is not at all clear what this is in reply to.

Thanks

Colin

Ok Colin,

about not top posting, is this top posting?

[long post intentionally not snipped so the comments at the end make sense]

> i need a join table? is the join table my comments table?

It already is a join table, you just need to tell rails that it can use it as such.

By the way could you not top post (so insert your comments at the appropriate point in the email you are replying to instead, as I have done here)? It makes it easier to follow the thread. For example your mail just says "I need a join table? .." but it is not at all clear what this is in reply to.

Thanks

Colin

>> > Oh ok thanks!

>> > But just to make sure we are on the same page-

>> > Page has many comments >> > Comments has many Davids

>> I believe that if you also say Page has_many davids through comments >> then you can get at all the davids for a post by >> @post.davids >> and hence you can use @posts.davids.count

>> Colin

>> > Would

>> > @davidcount = Post.comments.map {|comment| comment.davids.count}.sum

>> > find out how many David's are in a certain Post?

>> > Just to clarify, im not trying to find All of the David's that exsit >> > in the entire database, just the amount of Davids that belongs to the >> > post. But the part i dont understand is how to count up the David's >> > that belongs to that Post, because there are comments in between. >> > (post has many comments, comments has many davids) And i want to find >> > out for a certain post (@post) how many davds belongs to it)

>> > So would @davidcount = Post.comments.map {|comment| >> > comment.davids.count}.sum work for something like that?

>> > Thank you

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

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

>> - Show quoted text -

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

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

Ok Colin,

about not top posting, is this top posting?

No, it is bottom posting, which in this case is not much better. This can be seen from the fact that you had to preface your text with "about top posting", and that it is preceded by a large amount of irrelevant text. Had your reply read as follows it would be even clearer:

No. But it wasn't at the appropriate place in text. Making people scroll through pages of footers to get to your question won't incline them to answer you in future... just a thought.