Tom Ha wrote:
> Hi there,
> 
> I have the following tables...
> 
>   User :has_many Purchases
>   Item :has_many Purchases
> 
> 
> ...where Item has a column "amount" (can be + or -) and I need to find
> all Users that have a positive SUM of "amounts" (over all Purchases each
> one has made).
> 
> How does this query look like? (I'm not sure how to handle "SUM"
> correctly, in this case.)

@user # assume this exists
total = Purchase.sum(:amount, :conditions => { 'user_id' => @user })
=> SELECT sum("purchases".amount) AS sum_amount FROM "purchases" WHERE 
("purchases"."user_id" = 1)
-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to