Is it possible to do a find all, and then return the # of items?
David Z. 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.
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
On Apr 29, 2010, at 2:01 PM, Robert W. wrote:
David Z. 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.
On Mon, May 10, 2010 at 7:37 PM, David Z. [email protected]
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 thatHow 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: rubyredrick (Rick DeNatale) · GitHub
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
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
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
On May 11, 2010, at 7:47 AM, David Z. wrote:
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
@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
i need a join table? is the join table my comments table?
On 11 May 2010 13:32, David Z. [email protected] wrote:
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
On Tue, May 11, 2010 at 8:43 AM, David Z. [email protected]
wrote:
On May 11, 8:36 am, Colin L. [email protected] wrote:
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
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
–
Rick DeNatale
Blog: http://talklikeaduck.denhaven2.com/
Github: rubyredrick (Rick DeNatale) · GitHub
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
On May 11, 9:32 am, Colin L. [email protected] wrote:
your mail just says “I need a join table? …” but it is not at all
On May 11, 8:36 am, Colin L. [email protected] wrote:
then you can get at all the davids for a post by
Thank you
To unsubscribe from this group, send email to [email protected].
–
You received this message because you are subscribed to the Google G. “Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
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?
On 11 May 2010 13:43, David Z. [email protected] wrote:
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
On 11 May 2010 21:11, David Z. [email protected] wrote:
about not top posting, is this top posting?
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.
[long post intentionally not snipped so the comments at the end make
sense]
On 11 May 2010 21:11, David Z. [email protected] wrote:
By the way could you not top post (so insert your comments at the
Comments has many Davids
@davidcount = Post.comments.map {|comment| comment.davids.count}.sum
So would @davidcount = Post.comments.map {|comment|
–
To unsubscribe from this group, send email to [email protected].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:
On May 11, 9:32 am, Colin L. [email protected] wrote:
On 11 May 2010 13:43, David Z. [email protected] wrote:
[snip]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.
Ok Colin,
Is this top posting?