Created
January 30, 2021 13:13
-
-
Save Oceantidote/cdfb6f3d3f3bbc0feed37e4fd986c6c7 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 Ingredient | |
has_many :doses | |
belongs_to :alcohol | |
end | |
class Dose | |
belongs_to :ingredient | |
belongs_to :cocktail | |
end | |
class Cocktail | |
has_many :doses | |
has_many :ingredients, through: :doses | |
has_many :alcohols, through: :ingredients | |
end | |
class Alcohol | |
has_many :ingredients | |
end | |
ice = Ingredient.create(name: "ice") | |
cocktail = Cocktail.create(name: "mojito") | |
dose = Dose.new(description: "25ml", cocktail: cocktail, ingredient: ice) | |
cocktail.doses | |
cocktail.ingredients | |
# Cocktail has many ingredients | |
cocktail.dose.map do |dose| | |
dose.ingredient | |
end | |
# Cocktail has_many alcohols | |
cocktail.ingredients.map do |ingredient| | |
ingredient.alcohol | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment