Skip to content

Instantly share code, notes, and snippets.

@trev
Last active February 5, 2016 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trev/8d4d27903d2b3726832f to your computer and use it in GitHub Desktop.
Save trev/8d4d27903d2b3726832f to your computer and use it in GitHub Desktop.
OptimisePrime
class LineItem < ActiveRecord::Base
belongs_to :order
has_many :passengers
end
# RUSTY SPOON - BIG-O NO!
def unique_emails(order)
passengers = []
order.line_items.each do |li|
li.passengers.each do |pax|
passengers << pax.email
end
end
passengers.uniq!
end
# SLIGHTLY LESS RUSTY SPOON
def unique_emails(order)
Passenger.joins(line_item: :order)
.where(orders: { id: order })
.select(:email)
.distinct
.pluck(:email)
end
class Order < ActiveRecord::Base
has_many :line_items
end
class Passenger < ActiveRecord::Base
belongs_to :line_item
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment