Skip to content

Instantly share code, notes, and snippets.

@Oceantidote
Created January 30, 2021 13:13
Show Gist options
  • Save Oceantidote/cdfb6f3d3f3bbc0feed37e4fd986c6c7 to your computer and use it in GitHub Desktop.
Save Oceantidote/cdfb6f3d3f3bbc0feed37e4fd986c6c7 to your computer and use it in GitHub Desktop.
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