I have orders, order_items, and products. I want to collate several orders so that I can get a SUM of quantities ordered for each product etc. Can I say something like (the below gives an error on :sum, and ignores :group) OrderItem.find(:all, :sum => ''quantity'', :group => ''product_id'', :include => [:order, :product]) I want to get back a collection of order_items, GROUPed BY product_id. It is important for me to get back order_items, as then I can get to the product using the belongs_to association to get the name of the product, plus the business rules to calcualte the price of the product. Thanks Joerg -- Posted via http://www.ruby-forum.com/.
Check raisl 1.1 its has some very nice built-ins for this... On Tuesday, March 14, 2006, at 9:46 AM, Joerg Diekmann wrote:>I have orders, order_items, and products. > >I want to collate several orders so that I can get a SUM of quantities >ordered for each product etc. > >Can I say something like (the below gives an error on :sum, and ignores >:group) > >OrderItem.find(:all, :sum => ''quantity'', :group => ''product_id'', >:include => [:order, :product]) > >I want to get back a collection of order_items, GROUPed BY product_id. >It is important for me to get back order_items, as then I can get to the >product using the belongs_to association to get the name of the product, >plus the business rules to calcualte the price of the product. > >Thanks >Joerg > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsMikkel Bruun www.strongside.dk - Football Portal(DK) nflfeed.helenius.org - Football News(DK) ting.minline.dk - Buy Old Stuff!(DK) -- Posted with http://DevLists.com. Sign up and save your time!
I''m using EdgeRails, but that expression doesn''t work for me. Mikkel Bruun wrote:> Check raisl 1.1 its has some very nice built-ins for this... > > On Tuesday, March 14, 2006, at 9:46 AM, Joerg Diekmann wrote: >> >>_______________________________________________ >>Rails mailing list >>Rails@lists.rubyonrails.org >>http://lists.rubyonrails.org/mailman/listinfo/rails > > > Mikkel Bruun > > www.strongside.dk - Football Portal(DK) > nflfeed.helenius.org - Football News(DK) > ting.minline.dk - Buy Old Stuff!(DK)-- Posted via http://www.ruby-forum.com/.
Mikkel Bruun wrote: ...>>I want to get back a collection of order_items, GROUPed BY product_id. >>It is important for me to get back order_items, as then I can get to the >>product using the belongs_to association to get the name of the product, >>plus the business rules to calcualte the price of the product.... I wasted 3 hours yesterday trying to do this very same thing... Eventually I had to revert 3 hours of coding because I hit a brick wall ;-(
So - does anybody else know how to use :group and :sum in a find(:all) method? bmgz wrote:> Mikkel Bruun wrote: > ... >>>I want to get back a collection of order_items, GROUPed BY product_id. >>>It is important for me to get back order_items, as then I can get to the >>>product using the belongs_to association to get the name of the product, >>>plus the business rules to calcualte the price of the product. > ... > > I wasted 3 hours yesterday trying to do this very same thing... > Eventually I had to revert 3 hours of coding because I hit a > brick wall ;-(-- Posted via http://www.ruby-forum.com/.
In edgerails: http://edgedocs.planetargon.org/classes/ActiveRecord/Calculations/ ClassMethods.html#M004566 On Tuesday, March 14, 2006, at 9:46 AM, Joerg Diekmann wrote:>I have orders, order_items, and products. > >I want to collate several orders so that I can get a SUM of quantities >ordered for each product etc. > >Can I say something like (the below gives an error on :sum, and ignores >:group) > >OrderItem.find(:all, :sum => ''quantity'', :group => ''product_id'', >:include => [:order, :product]) > >I want to get back a collection of order_items, GROUPed BY product_id. >It is important for me to get back order_items, as then I can get to the >product using the belongs_to association to get the name of the product, >plus the business rules to calcualte the price of the product. > >Thanks >Joerg > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsMikkel Bruun www.strongside.dk - Football Portal(DK) nflfeed.helenius.org - Football News(DK) ting.minline.dk - Buy Old Stuff!(DK) -- Posted with http://DevLists.com. Sign up and save your time!