Created
August 11, 2014 17:45
-
-
Save jwo/f08b32f372fc6385acfd to your computer and use it in GitHub Desktop.
TIYA review of relationships
This file contains 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 | |
has_many :line_items | |
has_many :cupcakes, through: :line_items | |
def total_price | |
totals = line_items.map(&:total) # 50, 50, 100 | |
totals.sum | |
totals.inject(:+) | |
# total = 0 | |
# | |
# line_items.each do |li| # 50, 50, 100 | |
# total += li.total | |
# end | |
# | |
# total | |
end | |
end | |
class LineItem | |
belongs_to :order | |
belongs_to :cupcake | |
#quantity | |
#price | |
def total | |
quantity * price | |
end | |
end | |
class Cupcake | |
has_many :line_items | |
has_many :orders, through: :line_items | |
# name | |
end | |
-------- | |
# @order | |
puts @order.id | |
puts @order.created_at | |
"your cupcake selections: #{@order.cupcakes.map(&:name).join(", ")}" | |
@order.line_items.each do |line_item| | |
print line_item.quantity | |
print number_to_currency line_item.price | |
print line_item.cupcake.name | |
end | |
puts "total: #{@order.total_price}" | |
# sum all line_item totals (price * quantity) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment