rails 4.0.0 → 4.2.11.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +30 -23
  3. data/guides/CHANGELOG.md +108 -6
  4. data/guides/Rakefile +21 -6
  5. data/guides/assets/images/akshaysurve.jpg +0 -0
  6. data/guides/assets/images/edge_badge.png +0 -0
  7. data/guides/assets/images/feature_tile.gif +0 -0
  8. data/guides/assets/images/footer_tile.gif +0 -0
  9. data/guides/assets/images/fxn.png +0 -0
  10. data/guides/assets/images/getting_started/article_with_comments.png +0 -0
  11. data/guides/assets/images/getting_started/challenge.png +0 -0
  12. data/guides/assets/images/getting_started/confirm_dialog.png +0 -0
  13. data/guides/assets/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
  14. data/guides/assets/images/getting_started/form_with_errors.png +0 -0
  15. data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
  16. data/guides/assets/images/getting_started/new_article.png +0 -0
  17. data/guides/assets/images/getting_started/rails_welcome.png +0 -0
  18. data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
  19. data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
  20. data/guides/assets/images/getting_started/show_action_for_articles.png +0 -0
  21. data/guides/assets/images/getting_started/template_is_missing_articles_new.png +0 -0
  22. data/guides/assets/images/getting_started/unknown_action_create_for_articles.png +0 -0
  23. data/guides/assets/images/getting_started/unknown_action_new_for_articles.png +0 -0
  24. data/guides/assets/images/header_tile.gif +0 -0
  25. data/guides/assets/images/icons/README +1 -1
  26. data/guides/assets/images/icons/callouts/11.png +0 -0
  27. data/guides/assets/images/icons/callouts/12.png +0 -0
  28. data/guides/assets/images/icons/callouts/13.png +0 -0
  29. data/guides/assets/images/icons/callouts/15.png +0 -0
  30. data/guides/assets/images/icons/caution.png +0 -0
  31. data/guides/assets/images/icons/example.png +0 -0
  32. data/guides/assets/images/radar.png +0 -0
  33. data/guides/assets/images/rails4_features.png +0 -0
  34. data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
  35. data/guides/assets/images/vijaydev.jpg +0 -0
  36. data/guides/assets/javascripts/guides.js +36 -34
  37. data/guides/assets/stylesheets/main.css +6 -2
  38. data/guides/assets/stylesheets/print.css +1 -1
  39. data/guides/bug_report_templates/action_controller_gem.rb +47 -0
  40. data/guides/bug_report_templates/action_controller_master.rb +54 -0
  41. data/guides/bug_report_templates/active_record_gem.rb +5 -2
  42. data/guides/bug_report_templates/active_record_master.rb +3 -2
  43. data/guides/bug_report_templates/generic_gem.rb +15 -0
  44. data/guides/bug_report_templates/generic_master.rb +26 -0
  45. data/guides/rails_guides.rb +23 -4
  46. data/guides/rails_guides/generator.rb +1 -1
  47. data/guides/rails_guides/helpers.rb +4 -2
  48. data/guides/rails_guides/levenshtein.rb +27 -21
  49. data/guides/rails_guides/markdown.rb +11 -7
  50. data/guides/rails_guides/markdown/renderer.rb +1 -1
  51. data/guides/source/2_2_release_notes.md +3 -3
  52. data/guides/source/2_3_release_notes.md +12 -12
  53. data/guides/source/3_0_release_notes.md +10 -13
  54. data/guides/source/3_1_release_notes.md +7 -4
  55. data/guides/source/3_2_release_notes.md +17 -14
  56. data/guides/source/4_0_release_notes.md +110 -54
  57. data/guides/source/4_1_release_notes.md +730 -0
  58. data/guides/source/4_2_release_notes.md +877 -0
  59. data/guides/source/_license.html.erb +1 -1
  60. data/guides/source/_welcome.html.erb +6 -2
  61. data/guides/source/action_controller_overview.md +223 -57
  62. data/guides/source/action_mailer_basics.md +129 -76
  63. data/guides/source/action_view_overview.md +247 -246
  64. data/guides/source/active_job_basics.md +339 -0
  65. data/guides/source/active_model_basics.md +374 -20
  66. data/guides/source/active_record_basics.md +46 -45
  67. data/guides/source/active_record_callbacks.md +83 -28
  68. data/guides/source/{migrations.md → active_record_migrations.md} +191 -275
  69. data/guides/source/active_record_postgresql.md +433 -0
  70. data/guides/source/active_record_querying.md +382 -300
  71. data/guides/source/active_record_validations.md +64 -55
  72. data/guides/source/active_support_core_extensions.md +229 -187
  73. data/guides/source/active_support_instrumentation.md +23 -22
  74. data/guides/source/api_documentation_guidelines.md +167 -15
  75. data/guides/source/asset_pipeline.md +768 -294
  76. data/guides/source/association_basics.md +188 -96
  77. data/guides/source/autoloading_and_reloading_constants.md +1311 -0
  78. data/guides/source/caching_with_rails.md +45 -11
  79. data/guides/source/command_line.md +96 -65
  80. data/guides/source/configuring.md +404 -70
  81. data/guides/source/contributing_to_ruby_on_rails.md +270 -130
  82. data/guides/source/credits.html.erb +7 -3
  83. data/guides/source/debugging_rails_applications.md +471 -284
  84. data/guides/source/development_dependencies_install.md +115 -21
  85. data/guides/source/documents.yaml +31 -9
  86. data/guides/source/engines.md +737 -291
  87. data/guides/source/form_helpers.md +137 -89
  88. data/guides/source/generators.md +60 -28
  89. data/guides/source/getting_started.md +1007 -596
  90. data/guides/source/i18n.md +178 -96
  91. data/guides/source/index.html.erb +2 -1
  92. data/guides/source/initialization.md +248 -104
  93. data/guides/source/kindle/toc.html.erb +1 -1
  94. data/guides/source/layout.html.erb +14 -22
  95. data/guides/source/layouts_and_rendering.md +78 -46
  96. data/guides/source/maintenance_policy.md +78 -0
  97. data/guides/source/nested_model_forms.md +10 -7
  98. data/guides/source/plugins.md +66 -57
  99. data/guides/source/rails_application_templates.md +49 -12
  100. data/guides/source/rails_on_rack.md +50 -60
  101. data/guides/source/routing.md +190 -139
  102. data/guides/source/ruby_on_rails_guides_guidelines.md +12 -13
  103. data/guides/source/security.md +134 -83
  104. data/guides/source/testing.md +322 -200
  105. data/guides/source/upgrading_ruby_on_rails.md +834 -37
  106. data/guides/source/working_with_javascript_in_rails.md +36 -26
  107. data/guides/w3c_validator.rb +2 -0
  108. metadata +93 -116
  109. data/guides/assets/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
  110. data/guides/assets/images/getting_started/new_post.png +0 -0
  111. data/guides/assets/images/getting_started/post_with_comments.png +0 -0
  112. data/guides/assets/images/getting_started/show_action_for_posts.png +0 -0
  113. data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
  114. data/guides/assets/images/getting_started/undefined_method_post_path.png +0 -0
  115. data/guides/assets/images/getting_started/unknown_action_create_for_posts.png +0 -0
  116. data/guides/assets/images/getting_started/unknown_action_new_for_posts.png +0 -0
  117. data/guides/assets/images/jaimeiniesta.jpg +0 -0
  118. data/guides/code/getting_started/Gemfile +0 -43
  119. data/guides/code/getting_started/Gemfile.lock +0 -150
  120. data/guides/code/getting_started/README.rdoc +0 -28
  121. data/guides/code/getting_started/Rakefile +0 -6
  122. data/guides/code/getting_started/app/assets/javascripts/application.js +0 -16
  123. data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +0 -3
  124. data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +0 -3
  125. data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +0 -3
  126. data/guides/code/getting_started/app/assets/stylesheets/application.css +0 -13
  127. data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +0 -3
  128. data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +0 -3
  129. data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +0 -3
  130. data/guides/code/getting_started/app/controllers/application_controller.rb +0 -5
  131. data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -17
  132. data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -47
  133. data/guides/code/getting_started/app/controllers/welcome_controller.rb +0 -4
  134. data/guides/code/getting_started/app/helpers/application_helper.rb +0 -2
  135. data/guides/code/getting_started/app/helpers/comments_helper.rb +0 -2
  136. data/guides/code/getting_started/app/helpers/posts_helper.rb +0 -2
  137. data/guides/code/getting_started/app/helpers/welcome_helper.rb +0 -2
  138. data/guides/code/getting_started/app/models/comment.rb +0 -3
  139. data/guides/code/getting_started/app/models/post.rb +0 -7
  140. data/guides/code/getting_started/app/views/comments/_comment.html.erb +0 -15
  141. data/guides/code/getting_started/app/views/comments/_form.html.erb +0 -13
  142. data/guides/code/getting_started/app/views/layouts/application.html.erb +0 -14
  143. data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -27
  144. data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -5
  145. data/guides/code/getting_started/app/views/posts/index.html.erb +0 -21
  146. data/guides/code/getting_started/app/views/posts/new.html.erb +0 -5
  147. data/guides/code/getting_started/app/views/posts/show.html.erb +0 -18
  148. data/guides/code/getting_started/app/views/welcome/index.html.erb +0 -3
  149. data/guides/code/getting_started/bin/bundle +0 -4
  150. data/guides/code/getting_started/bin/rails +0 -4
  151. data/guides/code/getting_started/bin/rake +0 -4
  152. data/guides/code/getting_started/config.ru +0 -4
  153. data/guides/code/getting_started/config/application.rb +0 -18
  154. data/guides/code/getting_started/config/boot.rb +0 -4
  155. data/guides/code/getting_started/config/database.yml +0 -25
  156. data/guides/code/getting_started/config/environment.rb +0 -5
  157. data/guides/code/getting_started/config/environments/development.rb +0 -30
  158. data/guides/code/getting_started/config/environments/production.rb +0 -80
  159. data/guides/code/getting_started/config/environments/test.rb +0 -36
  160. data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
  161. data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +0 -4
  162. data/guides/code/getting_started/config/initializers/inflections.rb +0 -16
  163. data/guides/code/getting_started/config/initializers/locale.rb +0 -9
  164. data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
  165. data/guides/code/getting_started/config/initializers/secret_token.rb +0 -12
  166. data/guides/code/getting_started/config/initializers/session_store.rb +0 -3
  167. data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
  168. data/guides/code/getting_started/config/locales/en.yml +0 -23
  169. data/guides/code/getting_started/config/routes.rb +0 -7
  170. data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +0 -10
  171. data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +0 -11
  172. data/guides/code/getting_started/db/schema.rb +0 -33
  173. data/guides/code/getting_started/db/seeds.rb +0 -7
  174. data/guides/code/getting_started/public/404.html +0 -58
  175. data/guides/code/getting_started/public/422.html +0 -58
  176. data/guides/code/getting_started/public/500.html +0 -57
  177. data/guides/code/getting_started/public/favicon.ico +0 -0
  178. data/guides/code/getting_started/public/robots.txt +0 -5
  179. data/guides/code/getting_started/test/controllers/comments_controller_test.rb +0 -7
  180. data/guides/code/getting_started/test/controllers/posts_controller_test.rb +0 -7
  181. data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +0 -9
  182. data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
  183. data/guides/code/getting_started/test/fixtures/posts.yml +0 -9
  184. data/guides/code/getting_started/test/helpers/comments_helper_test.rb +0 -4
  185. data/guides/code/getting_started/test/helpers/posts_helper_test.rb +0 -4
  186. data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +0 -4
  187. data/guides/code/getting_started/test/models/comment_test.rb +0 -7
  188. data/guides/code/getting_started/test/models/post_test.rb +0 -7
  189. data/guides/code/getting_started/test/test_helper.rb +0 -15
  190. data/guides/source/kindle/KINDLE.md +0 -26
@@ -18,9 +18,10 @@ After reading this guide, you will know:
18
18
  Migration Overview
19
19
  ------------------
20
20
 
21
- Migrations are a convenient way to alter your database schema over time in a
22
- consistent and easy way. They use a Ruby DSL so that you don't have to write
23
- SQL by hand, allowing your schema and changes to be database independent.
21
+ Migrations are a convenient way to
22
+ [alter your database schema over time](http://en.wikipedia.org/wiki/Schema_migration)
23
+ in a consistent and easy way. They use a Ruby DSL so that you don't have to
24
+ write SQL by hand, allowing your schema and changes to be database independent.
24
25
 
25
26
  You can think of each migration as being a new 'version' of the database. A
26
27
  schema starts off with nothing in it, and each migration modifies it to add or
@@ -38,7 +39,7 @@ class CreateProducts < ActiveRecord::Migration
38
39
  t.string :name
39
40
  t.text :description
40
41
 
41
- t.timestamps
42
+ t.timestamps null: false
42
43
  end
43
44
  end
44
45
  end
@@ -120,7 +121,7 @@ Of course, calculating timestamps is no fun, so Active Record provides a
120
121
  generator to handle making it for you:
121
122
 
122
123
  ```bash
123
- $ rails generate migration AddPartNumberToProducts
124
+ $ bin/rails generate migration AddPartNumberToProducts
124
125
  ```
125
126
 
126
127
  This will create an empty but appropriately named migration:
@@ -137,7 +138,7 @@ followed by a list of column names and types then a migration containing the
137
138
  appropriate `add_column` and `remove_column` statements will be created.
138
139
 
139
140
  ```bash
140
- $ rails generate migration AddPartNumberToProducts part_number:string
141
+ $ bin/rails generate migration AddPartNumberToProducts part_number:string
141
142
  ```
142
143
 
143
144
  will generate
@@ -153,7 +154,7 @@ end
153
154
  If you'd like to add an index on the new column, you can do that as well:
154
155
 
155
156
  ```bash
156
- $ rails generate migration AddPartNumberToProducts part_number:string:index
157
+ $ bin/rails generate migration AddPartNumberToProducts part_number:string:index
157
158
  ```
158
159
 
159
160
  will generate
@@ -171,7 +172,7 @@ end
171
172
  Similarly, you can generate a migration to remove a column from the command line:
172
173
 
173
174
  ```bash
174
- $ rails generate migration RemovePartNumberFromProducts part_number:string
175
+ $ bin/rails generate migration RemovePartNumberFromProducts part_number:string
175
176
  ```
176
177
 
177
178
  generates
@@ -184,10 +185,10 @@ class RemovePartNumberFromProducts < ActiveRecord::Migration
184
185
  end
185
186
  ```
186
187
 
187
- You are not limited to one magically generated column. For example
188
+ You are not limited to one magically generated column. For example:
188
189
 
189
190
  ```bash
190
- $ rails generate migration AddDetailsToProducts part_number:string price:decimal
191
+ $ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
191
192
  ```
192
193
 
193
194
  generates
@@ -206,7 +207,7 @@ followed by a list of column names and types then a migration creating the table
206
207
  XXX with the columns listed will be generated. For example:
207
208
 
208
209
  ```bash
209
- $ rails generate migration CreateProducts name:string part_number:string
210
+ $ bin/rails generate migration CreateProducts name:string part_number:string
210
211
  ```
211
212
 
212
213
  generates
@@ -227,10 +228,10 @@ or remove from it as you see fit by editing the
227
228
  `db/migrate/YYYYMMDDHHMMSS_add_details_to_products.rb` file.
228
229
 
229
230
  Also, the generator accepts column type as `references`(also available as
230
- `belongs_to`). For instance
231
+ `belongs_to`). For instance:
231
232
 
232
233
  ```bash
233
- $ rails generate migration AddUserRefToProducts user:references
234
+ $ bin/rails generate migration AddUserRefToProducts user:references
234
235
  ```
235
236
 
236
237
  generates
@@ -248,7 +249,7 @@ This migration will create a `user_id` column and appropriate index.
248
249
  There is also a generator which will produce join tables if `JoinTable` is part of the name:
249
250
 
250
251
  ```bash
251
- rails g migration CreateJoinTableCustomerProduct customer product
252
+ $ bin/rails g migration CreateJoinTableCustomerProduct customer product
252
253
  ```
253
254
 
254
255
  will produce the following migration:
@@ -269,10 +270,10 @@ end
269
270
  The model and scaffold generators will create migrations appropriate for adding
270
271
  a new model. This migration will already contain instructions for creating the
271
272
  relevant table. If you tell Rails what columns you want, then statements for
272
- adding these columns will also be created. For example, running
273
+ adding these columns will also be created. For example, running:
273
274
 
274
275
  ```bash
275
- $ rails generate model Product name:string description:text
276
+ $ bin/rails generate model Product name:string description:text
276
277
  ```
277
278
 
278
279
  will create a migration that looks like this
@@ -284,7 +285,7 @@ class CreateProducts < ActiveRecord::Migration
284
285
  t.string :name
285
286
  t.text :description
286
287
 
287
- t.timestamps
288
+ t.timestamps null: false
288
289
  end
289
290
  end
290
291
  end
@@ -292,20 +293,15 @@ end
292
293
 
293
294
  You can append as many column name/type pairs as you want.
294
295
 
295
- ### Supported Type Modifiers
296
+ ### Passing Modifiers
296
297
 
297
- You can also specify some options just after the field type between curly
298
- braces. You can use the following modifiers:
298
+ Some commonly used [type modifiers](#column-modifiers) can be passed directly on
299
+ the command line. They are enclosed by curly braces and follow the field type:
299
300
 
300
- * `limit` Sets the maximum size of the `string/text/binary/integer` fields
301
- * `precision` Defines the precision for the `decimal` fields
302
- * `scale` Defines the scale for the `decimal` fields
303
- * `polymorphic` Adds a `type` column for `belongs_to` associations
304
-
305
- For instance, running
301
+ For instance, running:
306
302
 
307
303
  ```bash
308
- $ rails generate migration AddDetailsToProducts price:decimal{5,2} supplier:references{polymorphic}
304
+ $ bin/rails generate migration AddDetailsToProducts 'price:decimal{5,2}' supplier:references{polymorphic}
309
305
  ```
310
306
 
311
307
  will produce a migration that looks like this
@@ -313,12 +309,14 @@ will produce a migration that looks like this
313
309
  ```ruby
314
310
  class AddDetailsToProducts < ActiveRecord::Migration
315
311
  def change
316
- add_column :products, :price, precision: 5, scale: 2
317
- add_reference :products, :user, polymorphic: true, index: true
312
+ add_column :products, :price, :decimal, precision: 5, scale: 2
313
+ add_reference :products, :supplier, polymorphic: true, index: true
318
314
  end
319
315
  end
320
316
  ```
321
317
 
318
+ TIP: Have a look at the generators help output for further details.
319
+
322
320
  Writing a Migration
323
321
  -------------------
324
322
 
@@ -344,7 +342,7 @@ By default, `create_table` will create a primary key called `id`. You can change
344
342
  the name of the primary key with the `:primary_key` option (don't forget to
345
343
  update the corresponding model) or, if you don't want a primary key at all, you
346
344
  can pass the option `id: false`. If you need to pass database specific options
347
- you can place an SQL fragment in the `:options` option. For example,
345
+ you can place an SQL fragment in the `:options` option. For example:
348
346
 
349
347
  ```ruby
350
348
  create_table :products, options: "ENGINE=BLACKHOLE" do |t|
@@ -358,7 +356,7 @@ will append `ENGINE=BLACKHOLE` to the SQL statement used to create the table
358
356
  ### Creating a Join Table
359
357
 
360
358
  Migration method `create_join_table` creates a HABTM join table. A typical use
361
- would be
359
+ would be:
362
360
 
363
361
  ```ruby
364
362
  create_join_table :products, :categories
@@ -366,26 +364,24 @@ create_join_table :products, :categories
366
364
 
367
365
  which creates a `categories_products` table with two columns called
368
366
  `category_id` and `product_id`. These columns have the option `:null` set to
369
- `false` by default.
370
-
371
- You can pass the option `:table_name` with you want to customize the table
372
- name. For example,
367
+ `false` by default. This can be overridden by specifying the `:column_options`
368
+ option.
373
369
 
374
370
  ```ruby
375
- create_join_table :products, :categories, table_name: :categorization
371
+ create_join_table :products, :categories, column_options: {null: true}
376
372
  ```
377
373
 
378
- will create a `categorization` table.
374
+ will create the `product_id` and `category_id` with the `:null` option as
375
+ `true`.
379
376
 
380
- By default, `create_join_table` will create two columns with no options, but
381
- you can specify these options using the `:column_options` option. For example,
377
+ You can pass the option `:table_name` when you want to customize the table
378
+ name. For example:
382
379
 
383
380
  ```ruby
384
- create_join_table :products, :categories, column_options: {null: true}
381
+ create_join_table :products, :categories, table_name: :categorization
385
382
  ```
386
383
 
387
- will create the `product_id` and `category_id` with the `:null` option as
388
- `true`.
384
+ will create a `categorization` table.
389
385
 
390
386
  `create_join_table` also accepts a block, which you can use to add indices
391
387
  (which are not created by default) or additional columns:
@@ -401,7 +397,7 @@ end
401
397
 
402
398
  A close cousin of `create_table` is `change_table`, used for changing existing
403
399
  tables. It is used in a similar fashion to `create_table` but the object
404
- yielded to the block knows more tricks. For example
400
+ yielded to the block knows more tricks. For example:
405
401
 
406
402
  ```ruby
407
403
  change_table :products do |t|
@@ -415,13 +411,94 @@ end
415
411
  removes the `description` and `name` columns, creates a `part_number` string
416
412
  column and adds an index on it. Finally it renames the `upccode` column.
417
413
 
414
+ ### Changing Columns
415
+
416
+ Like the `remove_column` and `add_column` Rails provides the `change_column`
417
+ migration method.
418
+
419
+ ```ruby
420
+ change_column :products, :part_number, :text
421
+ ```
422
+
423
+ This changes the column `part_number` on products table to be a `:text` field.
424
+
425
+ Besides `change_column`, the `change_column_null` and `change_column_default`
426
+ methods are used specifically to change the null and default values of a
427
+ column.
428
+
429
+ ```ruby
430
+ change_column_null :products, :name, false
431
+ change_column_default :products, :approved, false
432
+ ```
433
+
434
+ This sets `:name` field on products to a `NOT NULL` column and the default
435
+ value of the `:approved` field to false.
436
+
437
+ TIP: Unlike `change_column` (and `change_column_default`), `change_column_null`
438
+ is reversible.
439
+
440
+ ### Column Modifiers
441
+
442
+ Column modifiers can be applied when creating or changing a column:
443
+
444
+ * `limit` Sets the maximum size of the `string/text/binary/integer` fields.
445
+ * `precision` Defines the precision for the `decimal` fields, representing the
446
+ total number of digits in the number.
447
+ * `scale` Defines the scale for the `decimal` fields, representing the
448
+ number of digits after the decimal point.
449
+ * `polymorphic` Adds a `type` column for `belongs_to` associations.
450
+ * `null` Allows or disallows `NULL` values in the column.
451
+ * `default` Allows to set a default value on the column. Note that if you
452
+ are using a dynamic value (such as a date), the default will only be calculated
453
+ the first time (i.e. on the date the migration is applied).
454
+ * `index` Adds an index for the column.
455
+ * `required` Adds `required: true` for `belongs_to` associations and
456
+ `null: false` to the column in the migration.
457
+
458
+ Some adapters may support additional options; see the adapter specific API docs
459
+ for further information.
460
+
461
+ ### Foreign Keys
462
+
463
+ While it's not required you might want to add foreign key constraints to
464
+ [guarantee referential integrity](#active-record-and-referential-integrity).
465
+
466
+ ```ruby
467
+ add_foreign_key :articles, :authors
468
+ ```
469
+
470
+ This adds a new foreign key to the `author_id` column of the `articles`
471
+ table. The key references the `id` column of the `authors` table. If the
472
+ column names can not be derived from the table names, you can use the
473
+ `:column` and `:primary_key` options.
474
+
475
+ Rails will generate a name for every foreign key starting with
476
+ `fk_rails_` followed by 10 random characters.
477
+ There is a `:name` option to specify a different name if needed.
478
+
479
+ NOTE: Active Record only supports single column foreign keys. `execute` and
480
+ `structure.sql` are required to use composite foreign keys.
481
+
482
+ Removing a foreign key is easy as well:
483
+
484
+ ```ruby
485
+ # let Active Record figure out the column name
486
+ remove_foreign_key :accounts, :branches
487
+
488
+ # remove foreign key for a specific column
489
+ remove_foreign_key :accounts, column: :owner_id
490
+
491
+ # remove foreign key by name
492
+ remove_foreign_key :accounts, name: :special_fk_name
493
+ ```
494
+
418
495
  ### When Helpers aren't Enough
419
496
 
420
497
  If the helpers provided by Active Record aren't enough you can use the `execute`
421
498
  method to execute arbitrary SQL:
422
499
 
423
500
  ```ruby
424
- Products.connection.execute('UPDATE `products` SET `price`=`free` WHERE 1')
501
+ Product.connection.execute('UPDATE `products` SET `price`=`free` WHERE 1')
425
502
  ```
426
503
 
427
504
  For more details and examples of individual methods, check the API documentation.
@@ -445,10 +522,11 @@ definitions:
445
522
  * `add_index`
446
523
  * `add_reference`
447
524
  * `add_timestamps`
525
+ * `add_foreign_key`
448
526
  * `create_table`
449
527
  * `create_join_table`
450
528
  * `drop_table` (must supply a block)
451
- * `drop_join_table` (must supply a block)
529
+ * `drop_join_table` (must supply a block)
452
530
  * `remove_timestamps`
453
531
  * `rename_column`
454
532
  * `rename_index`
@@ -465,29 +543,28 @@ or write the `up` and `down` methods instead of using the `change` method.
465
543
 
466
544
  Complex migrations may require processing that Active Record doesn't know how
467
545
  to reverse. You can use `reversible` to specify what to do when running a
468
- migration what else to do when reverting it. For example,
546
+ migration what else to do when reverting it. For example:
469
547
 
470
548
  ```ruby
471
549
  class ExampleMigration < ActiveRecord::Migration
472
550
  def change
473
- create_table :products do |t|
474
- t.references :category
551
+ create_table :distributors do |t|
552
+ t.string :zipcode
475
553
  end
476
554
 
477
555
  reversible do |dir|
478
556
  dir.up do
479
- #add a foreign key
557
+ # add a CHECK constraint
480
558
  execute <<-SQL
481
- ALTER TABLE products
482
- ADD CONSTRAINT fk_products_categories
483
- FOREIGN KEY (category_id)
484
- REFERENCES categories(id)
559
+ ALTER TABLE distributors
560
+ ADD CONSTRAINT zipchk
561
+ CHECK (char_length(zipcode) = 5) NO INHERIT;
485
562
  SQL
486
563
  end
487
564
  dir.down do
488
565
  execute <<-SQL
489
- ALTER TABLE products
490
- DROP FOREIGN KEY fk_products_categories
566
+ ALTER TABLE distributors
567
+ DROP CONSTRAINT zipchk
491
568
  SQL
492
569
  end
493
570
  end
@@ -495,12 +572,13 @@ class ExampleMigration < ActiveRecord::Migration
495
572
  add_column :users, :home_page_url, :string
496
573
  rename_column :users, :email, :email_address
497
574
  end
575
+ end
498
576
  ```
499
577
 
500
578
  Using `reversible` will ensure that the instructions are executed in the
501
579
  right order too. If the previous example migration is reverted,
502
580
  the `down` block will be run after the `home_page_url` column is removed and
503
- right before the table `products` is dropped.
581
+ right before the table `distributors` is dropped.
504
582
 
505
583
  Sometimes your migration will do something which is just plain irreversible; for
506
584
  example, it might destroy some data. In such cases, you can raise
@@ -523,16 +601,15 @@ made in the `up` method. The example in the `reversible` section is equivalent t
523
601
  ```ruby
524
602
  class ExampleMigration < ActiveRecord::Migration
525
603
  def up
526
- create_table :products do |t|
527
- t.references :category
604
+ create_table :distributors do |t|
605
+ t.string :zipcode
528
606
  end
529
607
 
530
- # add a foreign key
608
+ # add a CHECK constraint
531
609
  execute <<-SQL
532
- ALTER TABLE products
533
- ADD CONSTRAINT fk_products_categories
534
- FOREIGN KEY (category_id)
535
- REFERENCES categories(id)
610
+ ALTER TABLE distributors
611
+ ADD CONSTRAINT zipchk
612
+ CHECK (char_length(zipcode) = 5);
536
613
  SQL
537
614
 
538
615
  add_column :users, :home_page_url, :string
@@ -544,11 +621,11 @@ class ExampleMigration < ActiveRecord::Migration
544
621
  remove_column :users, :home_page_url
545
622
 
546
623
  execute <<-SQL
547
- ALTER TABLE products
548
- DROP FOREIGN KEY fk_products_categories
624
+ ALTER TABLE distributors
625
+ DROP CONSTRAINT zipchk
549
626
  SQL
550
627
 
551
- drop_table :products
628
+ drop_table :distributors
552
629
  end
553
630
  end
554
631
  ```
@@ -579,43 +656,27 @@ end
579
656
  The `revert` method also accepts a block of instructions to reverse.
580
657
  This could be useful to revert selected parts of previous migrations.
581
658
  For example, let's imagine that `ExampleMigration` is committed and it
582
- is later decided it would be best to serialize the product list instead.
583
- One could write:
659
+ is later decided it would be best to use Active Record validations,
660
+ in place of the `CHECK` constraint, to verify the zipcode.
584
661
 
585
662
  ```ruby
586
- class SerializeProductListMigration < ActiveRecord::Migration
663
+ class DontUseConstraintForZipcodeValidationMigration < ActiveRecord::Migration
587
664
  def change
588
- add_column :categories, :product_list
589
-
590
- reversible do |dir|
591
- dir.up do
592
- # transfer data from Products to Category#product_list
593
- end
594
- dir.down do
595
- # create Products from Category#product_list
596
- end
597
- end
598
-
599
665
  revert do
600
666
  # copy-pasted code from ExampleMigration
601
- create_table :products do |t|
602
- t.references :category
603
- end
604
-
605
667
  reversible do |dir|
606
668
  dir.up do
607
- #add a foreign key
669
+ # add a CHECK constraint
608
670
  execute <<-SQL
609
- ALTER TABLE products
610
- ADD CONSTRAINT fk_products_categories
611
- FOREIGN KEY (category_id)
612
- REFERENCES categories(id)
671
+ ALTER TABLE distributors
672
+ ADD CONSTRAINT zipchk
673
+ CHECK (char_length(zipcode) = 5);
613
674
  SQL
614
675
  end
615
676
  dir.down do
616
677
  execute <<-SQL
617
- ALTER TABLE products
618
- DROP FOREIGN KEY fk_products_categories
678
+ ALTER TABLE distributors
679
+ DROP CONSTRAINT zipchk
619
680
  SQL
620
681
  end
621
682
  end
@@ -643,16 +704,16 @@ method for all the migrations that have not yet been run. If there are
643
704
  no such migrations, it exits. It will run these migrations in order based
644
705
  on the date of the migration.
645
706
 
646
- Note that running the `db:migrate` also invokes the `db:schema:dump` task, which
707
+ Note that running the `db:migrate` task also invokes the `db:schema:dump` task, which
647
708
  will update your `db/schema.rb` file to match the structure of your database.
648
709
 
649
710
  If you specify a target version, Active Record will run the required migrations
650
711
  (change, up, down) until it has reached the specified version. The version
651
712
  is the numerical prefix on the migration's filename. For example, to migrate
652
- to version 20080906120000 run
713
+ to version 20080906120000 run:
653
714
 
654
715
  ```bash
655
- $ rake db:migrate VERSION=20080906120000
716
+ $ bin/rake db:migrate VERSION=20080906120000
656
717
  ```
657
718
 
658
719
  If version 20080906120000 is greater than the current version (i.e., it is
@@ -666,10 +727,10 @@ down to, but not including, 20080906120000.
666
727
 
667
728
  A common task is to rollback the last migration. For example, if you made a
668
729
  mistake in it and wish to correct it. Rather than tracking down the version
669
- number associated with the previous migration you can run
730
+ number associated with the previous migration you can run:
670
731
 
671
732
  ```bash
672
- $ rake db:rollback
733
+ $ bin/rake db:rollback
673
734
  ```
674
735
 
675
736
  This will rollback the latest migration, either by reverting the `change`
@@ -677,42 +738,47 @@ method or by running the `down` method. If you need to undo
677
738
  several migrations you can provide a `STEP` parameter:
678
739
 
679
740
  ```bash
680
- $ rake db:rollback STEP=3
741
+ $ bin/rake db:rollback STEP=3
681
742
  ```
682
743
 
683
744
  will revert the last 3 migrations.
684
745
 
685
746
  The `db:migrate:redo` task is a shortcut for doing a rollback and then migrating
686
747
  back up again. As with the `db:rollback` task, you can use the `STEP` parameter
687
- if you need to go more than one version back, for example
748
+ if you need to go more than one version back, for example:
688
749
 
689
750
  ```bash
690
- $ rake db:migrate:redo STEP=3
751
+ $ bin/rake db:migrate:redo STEP=3
691
752
  ```
692
753
 
693
754
  Neither of these Rake tasks do anything you could not do with `db:migrate`. They
694
755
  are simply more convenient, since you do not need to explicitly specify the
695
756
  version to migrate to.
696
757
 
758
+ ### Setup the Database
759
+
760
+ The `rake db:setup` task will create the database, load the schema and initialize
761
+ it with the seed data.
762
+
697
763
  ### Resetting the Database
698
764
 
699
- The `rake db:reset` task will drop the database, recreate it and load the
700
- current schema into it.
765
+ The `rake db:reset` task will drop the database and set it up again. This is
766
+ functionally equivalent to `rake db:drop db:setup`.
701
767
 
702
768
  NOTE: This is not the same as running all the migrations. It will only use the
703
- contents of the current schema.rb file. If a migration can't be rolled back,
704
- 'rake db:reset' may not help you. To find out more about dumping the schema see
705
- '[schema dumping and you](#schema-dumping-and-you).'
769
+ contents of the current `schema.rb` file. If a migration can't be rolled back,
770
+ `rake db:reset` may not help you. To find out more about dumping the schema see
771
+ [Schema Dumping and You](#schema-dumping-and-you) section.
706
772
 
707
773
  ### Running Specific Migrations
708
774
 
709
775
  If you need to run a specific migration up or down, the `db:migrate:up` and
710
776
  `db:migrate:down` tasks will do that. Just specify the appropriate version and
711
777
  the corresponding migration will have its `change`, `up` or `down` method
712
- invoked, for example,
778
+ invoked, for example:
713
779
 
714
780
  ```bash
715
- $ rake db:migrate:up VERSION=20080906120000
781
+ $ bin/rake db:migrate:up VERSION=20080906120000
716
782
  ```
717
783
 
718
784
  will run the 20080906120000 migration by running the `change` method (or the
@@ -728,7 +794,7 @@ To run migrations against another environment you can specify it using the
728
794
  migrations against the `test` environment you could run:
729
795
 
730
796
  ```bash
731
- $ rake db:migrate RAILS_ENV=test
797
+ $ bin/rake db:migrate RAILS_ENV=test
732
798
  ```
733
799
 
734
800
  ### Changing the Output of Running Migrations
@@ -751,7 +817,7 @@ Several methods are provided in migrations that allow you to control all this:
751
817
  | say | Takes a message argument and outputs it as is. A second boolean argument can be passed to specify whether to indent or not.
752
818
  | say_with_time | Outputs text along with how long it took to run its block. If the block returns an integer it assumes it is the number of rows affected.
753
819
 
754
- For example, this migration
820
+ For example, this migration:
755
821
 
756
822
  ```ruby
757
823
  class CreateProducts < ActiveRecord::Migration
@@ -760,7 +826,7 @@ class CreateProducts < ActiveRecord::Migration
760
826
  create_table :products do |t|
761
827
  t.string :name
762
828
  t.text :description
763
- t.timestamps
829
+ t.timestamps null: false
764
830
  end
765
831
  end
766
832
 
@@ -814,157 +880,6 @@ The `revert` method can be helpful when writing a new migration to undo
814
880
  previous migrations in whole or in part
815
881
  (see [Reverting Previous Migrations](#reverting-previous-migrations) above).
816
882
 
817
- Using Models in Your Migrations
818
- -------------------------------
819
-
820
- When creating or updating data in a migration it is often tempting to use one
821
- of your models. After all, they exist to provide easy access to the underlying
822
- data. This can be done, but some caution should be observed.
823
-
824
- For example, problems occur when the model uses database columns which are (1)
825
- not currently in the database and (2) will be created by this or a subsequent
826
- migration.
827
-
828
- Consider this example, where Alice and Bob are working on the same code base
829
- which contains a `Product` model:
830
-
831
- Bob goes on vacation.
832
-
833
- Alice creates a migration for the `products` table which adds a new column and
834
- initializes it. She also adds a validation to the `Product` model for the new
835
- column.
836
-
837
- ```ruby
838
- # db/migrate/20100513121110_add_flag_to_product.rb
839
-
840
- class AddFlagToProduct < ActiveRecord::Migration
841
- def change
842
- add_column :products, :flag, :boolean
843
- reversible do |dir|
844
- dir.up { Product.update_all flag: false }
845
- end
846
- Product.update_all flag: false
847
- end
848
- end
849
- ```
850
-
851
- ```ruby
852
- # app/models/product.rb
853
-
854
- class Product < ActiveRecord::Base
855
- validates :flag, presence: true
856
- end
857
- ```
858
-
859
- Alice adds a second migration which adds and initializes another column to the
860
- `products` table and also adds a validation to the `Product` model for the new
861
- column.
862
-
863
- ```ruby
864
- # db/migrate/20100515121110_add_fuzz_to_product.rb
865
-
866
- class AddFuzzToProduct < ActiveRecord::Migration
867
- def change
868
- add_column :products, :fuzz, :string
869
- reversible do |dir|
870
- dir.up { Product.update_all fuzz: 'fuzzy' }
871
- end
872
- end
873
- end
874
- ```
875
-
876
- ```ruby
877
- # app/models/product.rb
878
-
879
- class Product < ActiveRecord::Base
880
- validates :flag, :fuzz, presence: true
881
- end
882
- ```
883
-
884
- Both migrations work for Alice.
885
-
886
- Bob comes back from vacation and:
887
-
888
- * Updates the source - which contains both migrations and the latest version
889
- of the Product model.
890
- * Runs outstanding migrations with `rake db:migrate`, which
891
- includes the one that updates the `Product` model.
892
-
893
- The migration crashes because when the model attempts to save, it tries to
894
- validate the second added column, which is not in the database when the _first_
895
- migration runs:
896
-
897
- ```
898
- rake aborted!
899
- An error has occurred, this and all later migrations canceled:
900
-
901
- undefined method `fuzz' for #<Product:0x000001049b14a0>
902
- ```
903
-
904
- A fix for this is to create a local model within the migration. This keeps
905
- Rails from running the validations, so that the migrations run to completion.
906
-
907
- When using a local model, it's a good idea to call
908
- `Product.reset_column_information` to refresh the `ActiveRecord` cache for the
909
- `Product` model prior to updating data in the database.
910
-
911
- If Alice had done this instead, there would have been no problem:
912
-
913
- ```ruby
914
- # db/migrate/20100513121110_add_flag_to_product.rb
915
-
916
- class AddFlagToProduct < ActiveRecord::Migration
917
- class Product < ActiveRecord::Base
918
- end
919
-
920
- def change
921
- add_column :products, :flag, :boolean
922
- Product.reset_column_information
923
- reversible do |dir|
924
- dir.up { Product.update_all flag: false }
925
- end
926
- end
927
- end
928
- ```
929
-
930
- ```ruby
931
- # db/migrate/20100515121110_add_fuzz_to_product.rb
932
-
933
- class AddFuzzToProduct < ActiveRecord::Migration
934
- class Product < ActiveRecord::Base
935
- end
936
-
937
- def change
938
- add_column :products, :fuzz, :string
939
- Product.reset_column_information
940
- reversible do |dir|
941
- dir.up { Product.update_all fuzz: 'fuzzy' }
942
- end
943
- end
944
- end
945
- ```
946
-
947
- There are other ways in which the above example could have gone badly.
948
-
949
- For example, imagine that Alice creates a migration that selectively
950
- updates the `description` field on certain products. She runs the
951
- migration, commits the code, and then begins working on the next feature,
952
- which is to add a new column `fuzz` to the products table.
953
-
954
- She creates two migrations for this new feature, one which adds the new
955
- column, and a second which selectively updates the `fuzz` column based on
956
- other product attributes.
957
-
958
- These migrations run just fine, but when Bob comes back from his vacation
959
- and calls `rake db:migrate` to run all the outstanding migrations, he gets a
960
- subtle bug: The descriptions have defaults, and the `fuzz` column is present,
961
- but `fuzz` is nil on all products.
962
-
963
- The solution is again to use `Product.reset_column_information` before
964
- referencing the Product model in a migration, ensuring the Active Record's
965
- knowledge of the table structure is current before manipulating data in those
966
- records.
967
-
968
883
  Schema Dumping and You
969
884
  ----------------------
970
885
 
@@ -1026,16 +941,16 @@ that Active Record supports. This could be very useful if you were to
1026
941
  distribute an application that is able to run against multiple databases.
1027
942
 
1028
943
  There is however a trade-off: `db/schema.rb` cannot express database specific
1029
- items such as foreign key constraints, triggers, or stored procedures. While in
1030
- a migration you can execute custom SQL statements, the schema dumper cannot
1031
- reconstitute those statements from the database. If you are using features like
1032
- this, then you should set the schema format to `:sql`.
944
+ items such as triggers, or stored procedures. While in a migration you can
945
+ execute custom SQL statements, the schema dumper cannot reconstitute those
946
+ statements from the database. If you are using features like this, then you
947
+ should set the schema format to `:sql`.
1033
948
 
1034
949
  Instead of using Active Record's schema dumper, the database's structure will
1035
950
  be dumped using a tool specific to the database (via the `db:structure:dump`
1036
951
  Rake task) into `db/structure.sql`. For example, for PostgreSQL, the `pg_dump`
1037
- utility is used. For MySQL, this file will contain the output of `SHOW CREATE
1038
- TABLE` for the various tables.
952
+ utility is used. For MySQL, this file will contain the output of
953
+ `SHOW CREATE TABLE` for the various tables.
1039
954
 
1040
955
  Loading these schemas is simply a question of executing the SQL statements they
1041
956
  contain. By definition, this will create a perfect copy of the database's
@@ -1047,11 +962,16 @@ schema into a RDBMS other than the one used to create it.
1047
962
  Because schema dumps are the authoritative source for your database schema, it
1048
963
  is strongly recommended that you check them into source control.
1049
964
 
965
+ `db/schema.rb` contains the current version number of the database. This
966
+ ensures conflicts are going to happen in the case of a merge where both
967
+ branches touched the schema. When that happens, solve conflicts manually,
968
+ keeping the highest version number of the two.
969
+
1050
970
  Active Record and Referential Integrity
1051
971
  ---------------------------------------
1052
972
 
1053
973
  The Active Record way claims that intelligence belongs in your models, not in
1054
- the database. As such, features such as triggers or foreign key constraints,
974
+ the database. As such, features such as triggers or constraints,
1055
975
  which push some of that intelligence back into the database, are not heavily
1056
976
  used.
1057
977
 
@@ -1060,14 +980,10 @@ which models can enforce data integrity. The `:dependent` option on
1060
980
  associations allows models to automatically destroy child objects when the
1061
981
  parent is destroyed. Like anything which operates at the application level,
1062
982
  these cannot guarantee referential integrity and so some people augment them
1063
- with foreign key constraints in the database.
1064
-
1065
- Although Active Record does not provide any tools for working directly with
1066
- such features, the `execute` method can be used to execute arbitrary SQL. You
1067
- could also use some gem like
1068
- [foreigner](https://github.com/matthuhiggins/foreigner) which add foreign key
1069
- support to Active Record (including support for dumping foreign keys in
1070
- `db/schema.rb`).
983
+ with [foreign key constraints](#foreign-keys) in the database.
984
+
985
+ Although Active Record does not provide all the tools for working directly with
986
+ such features, the `execute` method can be used to execute arbitrary SQL.
1071
987
 
1072
988
  Migrations and Seed Data
1073
989
  ------------------------