By Lillian Ng
This application uses Rails 4.
This cookbook application lets you add and explore recipes. It is an example of a has_many :through relationship using nested forms. New recipes include fields to enter measurements and ingredients. Ingredients do not replicate if they are already in the database.
class Recipe < ActiveRecord::Base
has_many :measurements
has_many :ingredients, :through => :measurements
end
class Measurement < ActiveRecord::Base
belongs_to :recipe
belongs_to :ingredient
end
class Ingredient < ActiveRecord::Base
has_many :measurements
has_many :recipes, :through => :measurement
end
Recipe stores: name, servings, and directions
Measurement stores: ingredient_unit and ingredient_size
Ingredient stores: name and many booleans for tracking lactose, nut, shellfish, wheat, vegetarian, vegan
Tests have not been written :(
Nested forms have awkward behavior - sometimes the add link does not add fields to enter more ingredients
Different spelling/capitalization/plural ingredients will be replicated.
Ingredients that are linked to recipes can be deleted. This should not actually happen...
http://www.gitmatt.com/posts/5 - a starting point for has_many :through associations
From stackoverflow.com Rails has_many :through Nested Form