GROUP BY and SUM

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

Check raisl 1.1 its has some very nice built-ins for this…

On Tuesday, March 14, 2006, at 9:46 AM, Joerg D. wrote:


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Mikkel B.

www.strongside.dk - Football Portal(DK)
nflfeed.helenius.org - Football News(DK)
ting.minline.dk - Buy Old Stuff!(DK)

I’m using EdgeRails, but that expression doesn’t work for me.

Mikkel B. wrote:

Check raisl 1.1 its has some very nice built-ins for this…

On Tuesday, March 14, 2006, at 9:46 AM, Joerg D. wrote:


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Mikkel B.

www.strongside.dk - Football Portal(DK)
nflfeed.helenius.org - Football News(DK)
ting.minline.dk - Buy Old Stuff!(DK)

Mikkel B. 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 ;-(

In edgerails:
http://edgedocs.planetargon.org/classes/ActiveRecord/Calculations/
ClassMethods.html#M004566

On Tuesday, March 14, 2006, at 9:46 AM, Joerg D. wrote:


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Mikkel B.

www.strongside.dk - Football Portal(DK)
nflfeed.helenius.org - Football News(DK)
ting.minline.dk - Buy Old Stuff!(DK)

So - does anybody else know how to use :group and :sum in a find(:all)
method?

bmgz wrote:

Mikkel B. 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 ;-(

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs