Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Database
- create_table "ingredient_controllers", :force => true do |t|
- t.integer "recipe_id"
- t.integer "quantity_id"
- t.integer "ingredient_id"
- t.integer "preperation_id"
- end
- create_table "ingredients", :force => true do |t|
- t.integer "ingredient_type_id"
- t.string "name"
- t.date "created_on"
- end
- create_table "preperations", :force => true do |t|
- t.string "name"
- t.date "created_on"
- end
- create_table "quantities", :force => true do |t|
- t.date "created_on"
- t.string "name"
- end
- create_table "recipes", :force => true do |t|
- t.integer "user_id"
- t.string "name"
- t.text "description"
- t.date "created_on"
- t.date "updated_on"
- end
- ## Models
- class Ingredient < ActiveRecord::Base
- has_many :ingredient_controllers
- end
- class Preperation < ActiveRecord::Base
- has_many :ingredient_controllers
- end
- class Quantity < ActiveRecord::Base
- has_many :ingredient_controllers
- end
- class Recipe < ActiveRecord::Base
- has_many :comments
- has_many :ingredient_controllers
- has_many :ingredients, :through => :ingredient_controllers
- has_many :quantities, :through => :ingredient_controllers
- has_many :preperations, :through => :ingredient_controllers
- has_many :steps
- belongs_to :user
- has_and_belongs_to_many :tags
- end
- class IngredientController < ActiveRecord::Base
- belongs_to :recipe
- belongs_to :ingredient
- belongs_to :quantity
- belongs_to :preperation
- end
- ## Database call
- recipe = Recipe.find(1, :include=>[:ingredient_controllers])
- ## Results
- => #<Recipe id: 1, user_id: 1, name: "Clam Chowder", description: "Serves 6", created_on: "2008-03-08", updated_on: "2008-03-08">
- ## Data
- mysql> select * from recipes;
- +----+---------+--------------+-------------+------------+------------+
- | id | user_id | name | description | created_on | updated_on |
- +----+---------+--------------+-------------+------------+------------+
- | 1 | 1 | Clam Chowder | Serves 6 | 2008-03-08 | 2008-03-08 |
- +----+---------+--------------+-------------+------------+------------+
- 1 row in set (0.00 sec)
- mysql> select * from ingredient_controllers;
- +----+-----------+-------------+---------------+----------------+
- | id | recipe_id | quantity_id | ingredient_id | preperation_id |
- +----+-----------+-------------+---------------+----------------+
- | 1 | 1 | 8 | 11 | 2 |
- | 2 | 1 | 2 | 4 | 2 |
- | 3 | 1 | 3 | 5 | 3 |
- | 4 | 1 | 3 | 6 | 2 |
- | 5 | 1 | 4 | 7 | 4 |
- | 6 | 1 | 4 | 8 | 5 |
- | 7 | 1 | 5 | 9 | 4 |
- | 8 | 1 | 6 | 10 | 4 |
- +----+-----------+-------------+---------------+----------------+
- 8 rows in set (0.00 sec)
Add Comment
Please, Sign In to add comment