Forum: Ruby on Rails GROUP BY and SUM

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
34791e1d8c4def65f9b93294638a7e04?d=identicon&s=25 Joerg Diekmann (joergd)
on 2006-03-14 09:46
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
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Mikkel Bruun (Guest)
on 2006-03-14 09:50
(Received via mailing list)
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)
34791e1d8c4def65f9b93294638a7e04?d=identicon&s=25 Joerg Diekmann (joergd)
on 2006-03-14 09:53
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)
494e0313d09e2b405fcc19c7405239a4?d=identicon&s=25 bmgz (Guest)
on 2006-03-14 10:09
(Received via mailing list)
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 ;-(
34791e1d8c4def65f9b93294638a7e04?d=identicon&s=25 Joerg Diekmann (joergd)
on 2006-03-14 14:15
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 ;-(
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Mikkel Bruun (Guest)
on 2006-03-14 14:42
(Received via mailing list)
In edgerails:
http://edgedocs.planetargon.org/classes/ActiveReco...
ClassMethods.html#M004566


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)
This topic is locked and can not be replied to.