Skip to content
This repository
Browse code

Merge pull request #8432 from rmcastil/add_migration_example_to_each_…

…association_type

Add migration examples to Association Basics

[ci skip]
  • Loading branch information...
commit c5fb487965b9768da0e48a9348591422ec26f566 2 parents ae68fc3 + 312234c
Rafael Mendonça França authored December 13, 2012

Showing 1 changed file with 130 additions and 0 deletions. Show diff stats Hide diff stats

  1. 130  guides/source/association_basics.md
130  guides/source/association_basics.md
Source Rendered
@@ -94,6 +94,25 @@ end
94 94
 
95 95
 NOTE: `belongs_to` associations _must_ use the singular term. If you used the pluralized form in the above example for the `customer` association in the `Order` model, you would be told that there was an "uninitialized constant Order::Customers". This is because Rails automatically infers the class name from the association name. If the association name is wrongly pluralized, then the inferred class will be wrongly pluralized too.
96 96
 
  97
+The corresponding migration might look like this:
  98
+
  99
+```ruby
  100
+class CreateOrders < ActiveRecord::Migration
  101
+  def change
  102
+    create_table :customers do |t|
  103
+      t.string :name
  104
+      t.timestamps
  105
+    end
  106
+
  107
+    create_table :orders do |t|
  108
+      t.belongs_to :customer
  109
+      t.datetime :order_date
  110
+      t.timestamps
  111
+    end
  112
+  end
  113
+end
  114
+```
  115
+
97 116
 ### The `has_one` Association
98 117
 
99 118
 A `has_one` association also sets up a one-to-one connection with another model, but with somewhat different semantics (and consequences). This association indicates that each instance of a model contains or possesses one instance of another model. For example, if each supplier in your application has only one account, you'd declare the supplier model like this:
@@ -106,6 +125,25 @@ end
106 125
 
107 126
 ![has_one Association Diagram](images/has_one.png)
108 127
 
  128
+The corresponding migration might look like this:
  129
+
  130
+```ruby
  131
+class CreateSuppliers < ActiveRecord::Migration
  132
+  def change
  133
+    create_table :suppliers do |t|
  134
+      t.string :name
  135
+      t.timestamps
  136
+    end
  137
+
  138
+    create_table :accounts do |t|
  139
+      t.belongs_to :supplier
  140
+      t.string :account_number
  141
+      t.timestamps
  142
+    end
  143
+  end
  144
+end
  145
+```
  146
+
109 147
 ### The `has_many` Association
110 148
 
111 149
 A `has_many` association indicates a one-to-many connection with another model. You'll often find this association on the "other side" of a `belongs_to` association. This association indicates that each instance of the model has zero or more instances of another model. For example, in an application containing customers and orders, the customer model could be declared like this:
@@ -120,6 +158,25 @@ NOTE: The name of the other model is pluralized when declaring a `has_many` asso
120 158
 
121 159
 ![has_many Association Diagram](images/has_many.png)
122 160
 
  161
+The corresponding migration might look like this:
  162
+
  163
+```ruby
  164
+class CreateCustomers < ActiveRecord::Migration
  165
+  def change
  166
+    create_table :customers do |t|
  167
+      t.string :name
  168
+      t.timestamps
  169
+    end
  170
+
  171
+    create_table :orders do |t|
  172
+      t.belongs_to :customer
  173
+      t.datetime :order_date
  174
+      t.timestamps
  175
+    end
  176
+  end
  177
+end
  178
+```
  179
+
123 180
 ### The `has_many :through` Association
124 181
 
125 182
 A `has_many :through` association is often used to set up a many-to-many connection with another model. This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding _through_ a third model. For example, consider a medical practice where patients make appointments to see physicians. The relevant association declarations could look like this:
@@ -143,6 +200,31 @@ end
143 200
 
144 201
 ![has_many :through Association Diagram](images/has_many_through.png)
145 202
 
  203
+The corresponding migration might look like this:
  204
+
  205
+```ruby
  206
+class CreateAppointments < ActiveRecord::Migration
  207
+  def change
  208
+    create_table :physicians do |t|
  209
+      t.string :name
  210
+      t.timestamps
  211
+    end
  212
+
  213
+    create_table :patients do |t|
  214
+      t.string :name
  215
+      t.timestamps
  216
+    end
  217
+
  218
+    create_table :appointments do |t|
  219
+      t.belongs_to :physician
  220
+      t.belongs_to :patient
  221
+      t.datetime :appointment_date
  222
+      t.timestamps
  223
+    end
  224
+  end
  225
+end
  226
+```
  227
+
146 228
 The collection of join models can be managed via the API. For example, if you assign
147 229
 
148 230
 ```ruby
@@ -199,6 +281,31 @@ end
199 281
 
200 282
 ![has_one :through Association Diagram](images/has_one_through.png)
201 283
 
  284
+The corresponding migration might look like this:
  285
+
  286
+```ruby
  287
+class CreateAccountHistories < ActiveRecord::Migration
  288
+  def change
  289
+    create_table :suppliers do |t|
  290
+      t.string :name
  291
+      t.timestamps
  292
+    end
  293
+
  294
+    create_table :accounts do |t|
  295
+      t.belongs_to :supplier
  296
+      t.string :account_number
  297
+      t.timestamps
  298
+    end
  299
+
  300
+    create_table :account_histories do |t|
  301
+      t.belongs_to :account
  302
+      t.integer :credit_rating
  303
+      t.timestamps
  304
+    end
  305
+  end
  306
+end
  307
+```
  308
+
202 309
 ### The `has_and_belongs_to_many` Association
203 310
 
204 311
 A `has_and_belongs_to_many` association creates a direct many-to-many connection with another model, with no intervening model. For example, if your application includes assemblies and parts, with each assembly having many parts and each part appearing in many assemblies, you could declare the models this way:
@@ -215,6 +322,29 @@ end
215 322
 
216 323
 ![has_and_belongs_to_many Association Diagram](images/habtm.png)
217 324
 
  325
+The corresponding migration might look like this:
  326
+
  327
+```ruby
  328
+class CreateAssembliesAndParts < ActiveRecord::Migration
  329
+  def change
  330
+    create_table :assemblies do |t|
  331
+      t.string :name
  332
+      t.timestamps
  333
+    end
  334
+
  335
+    create_table :parts do |t|
  336
+      t.string :part_number
  337
+      t.timestamps
  338
+    end
  339
+
  340
+    create_table :assemblies_parts do |t|
  341
+      t.belongs_to :assembly
  342
+      t.belongs_to :part
  343
+    end
  344
+  end
  345
+end
  346
+```
  347
+
218 348
 ### Choosing Between `belongs_to` and `has_one`
219 349
 
220 350
 If you want to set up a one-to-one relationship between two models, you'll need to add `belongs_to` to one, and `has_one` to the other. How do you know which is which?

0 notes on commit c5fb487

Please sign in to comment.
Something went wrong with that request. Please try again.