Skip to content

Instantly share code, notes, and snippets.

@rajeevriitm
Last active September 16, 2016 15:55
Show Gist options
  • Save rajeevriitm/ad0da69b2b2a9b352a30de67581c911f to your computer and use it in GitHub Desktop.
Save rajeevriitm/ad0da69b2b2a9b352a30de67581c911f to your computer and use it in GitHub Desktop.

I have moved the method to customer model.

joins - for inner join to avoid customers witout order

includes - for eager loading

then sum to return array

class Order < ApplicationRecord
belongs_to :customer
end
class Customer < ApplicationRecord
has_many :orders
def totals_by_customer
Student.joins(:orders).includes(:orders).map { |student|
[ student , student.orders.sum(:total) ]
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment