GROUP BY and SUM


#1

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


#2

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
removed_email_address@domain.invalid
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)


#3

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
removed_email_address@domain.invalid
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)


#4

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 ;-(


#5

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
removed_email_address@domain.invalid
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)


#6

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 ;-(