hello all, so i have the following situation company has_many products products has many parts i can do company.products .count and get back the product count what i''d like to know is if there is a more elegant way to get the number of parts for a given company outside of iterating through each product, counting its parts and then summing that number. the solution i have now works. i was just wondering if there was a more elegant way of going about it. thanks, binh -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Here''s one way to do it: Part.count :conditions => [''product_id in (select id from products where company_id = ?)'', company_id] -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Binh Ly wrote:> hello all, > > so i have the following situation > > company has_many products > products has many parts > > i can do company.products .count and get back the product count > > what i''d like to know is if there is a more elegant way to get the > number of parts for a given company outside of iterating through each > product, counting its parts and then summing that number. > > the solution i have now works. i was just wondering if there was a more > elegant way of going about it. > > thanks, > > binhWithin rails? probably not. You could, however, create a view in the target database and then use that to return the desired values. I Or, if this type of reporting is a frequent requirement, and the the parts per product is usually stable, then it might be worth while to have a regularly scheduled or dynamically calculated roll-up of the part count into a field in the product table and then report on that. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
wouldn''t the counter_cache improve your performance? http://railscasts.com/episodes/23 at least for having only to read the parts_count from the product where company_id = ? select parts_count from prodcuts where company_id = ? and after that sum them up ;) should be one statement and an sum up. optimization possible. that''s how i would try to do it ;) On Jan 11, 9:45 pm, James Byrne <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Binh Ly wrote: > > hello all, > > > so i have the following situation > > > company has_many products > > products has many parts > > > i can do company.products .count and get back the product count > > > what i''d like to know is if there is a more elegant way to get the > > number of parts for a given company outside of iterating through each > > product, counting its parts and then summing that number. > > > the solution i have now works. i was just wondering if there was a more > > elegant way of going about it. > > > thanks, > > > binh > > Within rails? probably not. > > You could, however, create a view in the target database and then use > that to return the desired values. I > > Or, if this type of reporting is a frequent requirement, and the the > parts per product is usually stable, then it might be worth while to > have a regularly scheduled or dynamically calculated roll-up of the part > count into a field in the product table and then report on that. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 11, 12:34 pm, Binh Ly <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > so i have the following situation > > company has_many products > products has many parts > > i can do company.products .count and get back the product count > > what i''d like to know is if there is a more elegant way to get the > number of parts for a given company outside of iterating through each > product, counting its parts and then summing that number.Give Company a has_many :through relation to Part, and then you''ll be able to do company.parts.count. ///ark> > the solution i have now works. i was just wondering if there was a more > elegant way of going about it. > > thanks, > > binh > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---