Rails: sum all the elements of a column, where param1 == some value

I want to summarize all the elements (for example, price) from a table column, where some values ​​(type) are equal to some parameter (1):

Table

id type price ------------- 1 1 10 2 1 8 3 2 7 4 1 2 

I think it could be like this:

 Table.where(:type => 1). ??? 

I need to get 20.

+4
source share
1 answer
 Table.where(:type => 1).sum(:price) 
+12
source

All Articles