Type a keyword and hit enter to start searching. Press Esc to cancel.

Posts Tagged ‘sum’

ActiveRecord is great.  It helps hide away a lot of trivial SQL when creating applications with Rails.  It’s fairly good at what it does to and it’s constantly growing and improving.  However, it falls short when it comes to creating a query that combines multiple calculations.  As an example, let’s get an array of the total sales for every month for the last 12 months of Orders. totals = [] 12.times do |i| month = i.months.ago totals << Order.where(‘created_at >= ? AND created_at <= ?’, month.beginning_of_month, month.end_of_month).sum(:total) end This is going to run 12 queries in order to get the […]