Skip to content

Instantly share code, notes, and snippets.

@dsasse07
Last active December 29, 2020 22:24
Show Gist options
  • Save dsasse07/aad778b80069ba1d2e62e37f5ac14d32 to your computer and use it in GitHub Desktop.
Save dsasse07/aad778b80069ba1d2e62e37f5ac14d32 to your computer and use it in GitHub Desktop.
Example of literally defined aggregate methods for use in blog post.
def self.count_vhs_by_rentals
Rental.all.each_with_object({}) do |rental, vhs_hash|
vhs_hash[rental.vhs].nil? ? vhs_hash[rental.vhs] = 1 : vhs_hash[rental.vhs] += 1
end
end
#=> {<Vhs:0x00007f8d5e2bf0f0 id: 17, serial_number: "MAX-luqovgdk8e7tl", movie_id: 9 => 1,
#<Vhs:0x00007f8d4f81a4a0 id: 29, serial_number: "BABA-t6k24krzqclts", movie_id: 13 =>2,
#<Vhs:0x00007f8d4f854538 id: 11, serial_number: "BABA-begvxe7ai0rc6", movie_id: 13 =>3}
def self.count_vhs_by_client
Rental.all.each_with_object({}) do |rental, vhs_hash|
vhs_hash[rental.client].nil? ? vhs_hash[rental.client] = 1 : vhs_hash[rental.client] += 1
end
end
#=> {#<Client:0x00007f872c1bb4c0 id: 4, name: "Hank Jennings" =>1,
#<Client:0x00007f872c1c8fd0 id: 7, name: "Lisa Wilkes" =>2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment