Skip to content

Instantly share code, notes, and snippets.

@erikpukinskis
Created June 12, 2011 14:59
Show Gist options
  • Save erikpukinskis/1021637 to your computer and use it in GitHub Desktop.
Save erikpukinskis/1021637 to your computer and use it in GitHub Desktop.
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