daily pastebin goal
67%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 128 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Database
  2.   create_table "ingredient_controllers", :force => true do |t|
  3.     t.integer "recipe_id"
  4.     t.integer "quantity_id"
  5.     t.integer "ingredient_id"
  6.     t.integer "preperation_id"
  7.   end
  8.  
  9.   create_table "ingredients", :force => true do |t|
  10.     t.integer "ingredient_type_id"
  11.     t.string  "name"
  12.     t.date    "created_on"
  13.   end
  14.  
  15.   create_table "preperations", :force => true do |t|
  16.     t.string "name"
  17.     t.date   "created_on"
  18.   end
  19.  
  20.   create_table "quantities", :force => true do |t|
  21.     t.date   "created_on"
  22.     t.string "name"
  23.   end
  24.  
  25.   create_table "recipes", :force => true do |t|
  26.     t.integer "user_id"
  27.     t.string  "name"
  28.     t.text    "description"
  29.     t.date    "created_on"
  30.     t.date    "updated_on"
  31.   end
  32.  
  33. ## Models
  34. class Ingredient < ActiveRecord::Base
  35.   has_many :ingredient_controllers
  36. end
  37. class Preperation < ActiveRecord::Base
  38.   has_many :ingredient_controllers
  39. end
  40. class Quantity < ActiveRecord::Base
  41.   has_many :ingredient_controllers
  42. end
  43. class Recipe < ActiveRecord::Base
  44.   has_many :comments
  45.   has_many :ingredient_controllers
  46.   has_many :ingredients, :through => :ingredient_controllers
  47.   has_many :quantities, :through => :ingredient_controllers
  48.   has_many :preperations, :through => :ingredient_controllers
  49.   has_many :steps
  50.   belongs_to :user
  51.   has_and_belongs_to_many :tags
  52. end
  53. class IngredientController < ActiveRecord::Base
  54.   belongs_to :recipe
  55.   belongs_to :ingredient
  56.   belongs_to :quantity
  57.   belongs_to :preperation
  58. end
  59.  
  60. ## Database call
  61. recipe = Recipe.find(1, :include=>[:ingredient_controllers])
  62.  
  63. ## Results
  64. => #<Recipe id: 1, user_id: 1, name: "Clam Chowder", description: "Serves 6", created_on: "2008-03-08", updated_on: "2008-03-08">
  65.  
  66. ## Data
  67. mysql> select * from recipes;
  68. +----+---------+--------------+-------------+------------+------------+
  69. | id | user_id | name         | description | created_on | updated_on |
  70. +----+---------+--------------+-------------+------------+------------+
  71. |  1 |       1 | Clam Chowder | Serves 6    | 2008-03-08 | 2008-03-08 |
  72. +----+---------+--------------+-------------+------------+------------+
  73. 1 row in set (0.00 sec)
  74.  
  75. mysql> select * from ingredient_controllers;
  76. +----+-----------+-------------+---------------+----------------+
  77. | id | recipe_id | quantity_id | ingredient_id | preperation_id |
  78. +----+-----------+-------------+---------------+----------------+
  79. |  1 |         1 |           8 |            11 |              2 |
  80. |  2 |         1 |           2 |             4 |              2 |
  81. |  3 |         1 |           3 |             5 |              3 |
  82. |  4 |         1 |           3 |             6 |              2 |
  83. |  5 |         1 |           4 |             7 |              4 |
  84. |  6 |         1 |           4 |             8 |              5 |
  85. |  7 |         1 |           5 |             9 |              4 |
  86. |  8 |         1 |           6 |            10 |              4 |
  87. +----+-----------+-------------+---------------+----------------+
  88. 8 rows in set (0.00 sec)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top