rails summing column values of rows with similar attributes
Posted
by
butterywombat
on Stack Overflow
See other posts from Stack Overflow
or by butterywombat
Published on 2011-01-12T22:51:09Z
Indexed on
2011/01/12
22:53 UTC
Read the original article
Hit count: 129
ruby-on-rails
Hi all, I have a Sites table that has columns name, and time. The name does not have to be unique. So for example I may have the entries 'hi.com, 5', 'hi.com, 10', 'bye.com, 4'. I would like to sum up all the unique sites so that i get 'hi.com, 15' and 'bye.com, 4' for plotting purposes. How can I do that? (For some reference I was looking at http://railscasts.com/episodes/223-charts but I couldn't get the following (translated to my table) to work
def self.total_on(date)
where("date(purchased_at) = ?", date).sum(:total_price)
end
nor do I really understand the syntax of the 'where("date(purchased_at) = ?", date)
' part.
Thanks for helping a rails newbie!
© Stack Overflow or respective owner