Re: [Rails] Re: Find ALL and then count them up?

2010-05-11 Thread Rick DeNatale
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

[Rails] Re: Find ALL and then count them up?

2010-05-11 Thread David Zhu
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

Re: [Rails] Re: Find ALL and then count them up?

2010-05-11 Thread Rob Biedenharn
On May 11, 2010, at 7:47 AM, David Zhu 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?

[Rails] Re: Find ALL and then count them up?

2010-05-11 Thread David Zhu
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

Re: [Rails] Re: Find ALL and then count them up?

2010-05-11 Thread Colin Law
On 11 May 2010 13:32, David Zhu dzwestwindso...@gmail.com 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

[Rails] Re: Find ALL and then count them up?

2010-05-11 Thread David Zhu
i need a join table? is the join table my comments table? On May 11, 8:36 am, Colin Law clan...@googlemail.com wrote: On 11 May 2010 13:32, David Zhu dzwestwindso...@gmail.com wrote: Oh ok thanks! But just to make sure we are on the same page- Page has many comments Comments has many

Re: [Rails] Re: Find ALL and then count them up?

2010-05-11 Thread Rick DeNatale
On Tue, May 11, 2010 at 8:43 AM, David Zhu dzwestwindso...@gmail.com wrote: On May 11, 8:36 am, Colin Law clan...@googlemail.com 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

Re: [Rails] Re: Find ALL and then count them up?

2010-05-11 Thread Colin Law
On 11 May 2010 13:43, David Zhu dzwestwindso...@gmail.com 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

[Rails] Re: Find ALL and then count them up?

2010-05-11 Thread David Zhu
On May 11, 9:32 am, Colin Law clan...@googlemail.com wrote: On 11 May 2010 13:43, David Zhu dzwestwindso...@gmail.com 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

Re: [Rails] Re: Find ALL and then count them up?

2010-05-11 Thread Colin Law
[long post intentionally not snipped so the comments at the end make sense] On 11 May 2010 21:11, David Zhu dzwestwindso...@gmail.com wrote: On May 11, 9:32 am, Colin Law clan...@googlemail.com wrote: On 11 May 2010 13:43, David Zhu dzwestwindso...@gmail.com wrote: i need a join table? is

Re: [Rails] Re: Find ALL and then count them up?

2010-05-11 Thread Michael Pavling
On 11 May 2010 21:11, David Zhu dzwestwindso...@gmail.com 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. --

[Rails] Re: Find ALL and then count them up?

2010-05-10 Thread David Zhu
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, 5:07 pm, Philip Hallstrom

[Rails] Re: Find ALL and then count them up?

2010-04-29 Thread Robert Walker
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

Re: [Rails] Re: Find ALL and then count them up?

2010-04-29 Thread Philip Hallstrom
On Apr 29, 2010, at 2:01 PM, Robert Walker wrote: 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