Last active
February 5, 2016 05:11
-
-
Save trev/8d4d27903d2b3726832f to your computer and use it in GitHub Desktop.
OptimisePrime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LineItem < ActiveRecord::Base | |
belongs_to :order | |
has_many :passengers | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Order < ActiveRecord::Base | |
has_many :line_items | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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