Created
June 12, 2011 14:59
-
-
Save erikpukinskis/1021637 to your computer and use it in GitHub Desktop.
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 :foods, :through => :line_items | |
has_many :merchandise_items, :through => :line_items | |
has_many :line_items | |
def total | |
line_items.map(&:price).sum | |
end | |
end | |
class LineItem | |
belongs_to :food | |
belongs_to :merchandise_item | |
def price | |
food ? 100 : merchandise_item.price | |
end | |
end | |
receipt = Receipt.new | |
receipt.foods << Food.find("sandwich") | |
receipt.foods.first # This is #<Food id: "sandwich"> as it should be | |
receipt.line_items # this is unfortunately empty until we save the order! | |
receipt.total # this is zero, because there are no invoice items! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment