Skip to content

Instantly share code, notes, and snippets.

@kpheasey
Created September 15, 2016 21:24
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Speed Test
class Order < ApplicationRecord
belongs_to :customer
def totals_by_customer
Order.all.pluck(:customer).distinct.map do |customer|
[customer, customer.orders.sum(:total))]
end
end
end
class Customer < ApplicationRecord
has_many :orders
end
CREATE TABLE `orders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`customer_id` int(11) DEFAULT NULL,
`total` int(11) DEFAULT NULL,
);
CREATE TABLE `customers` (
`id` int(11) NOT NULL AUTO_INCREMENT
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment