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
@@ -40,7 +40,7 @@ end
40
40
  @customer.destroy
41
41
  ```
42
42
 
43
- With Active Record associations, we can streamline these and other operations by declaratively telling Rails that there is a connection between the two models. Here's the revised code for setting up customers and orders:
43
+ With Active Record associations, we can streamline these - and other - operations by declaratively telling Rails that there is a connection between the two models. Here's the revised code for setting up customers and orders:
44
44
 
45
45
  ```ruby
46
46
  class Customer < ActiveRecord::Base
@@ -69,7 +69,7 @@ To learn more about the different types of associations, read the next section o
69
69
  The Types of Associations
70
70
  -------------------------
71
71
 
72
- In Rails, an _association_ is a connection between two Active Record models. Associations are implemented using macro-style calls, so that you can declaratively add features to your models. For example, by declaring that one model `belongs_to` another, you instruct Rails to maintain Primary KeyForeign Key information between instances of the two models, and you also get a number of utility methods added to your model. Rails supports six types of associations:
72
+ In Rails, an _association_ is a connection between two Active Record models. Associations are implemented using macro-style calls, so that you can declaratively add features to your models. For example, by declaring that one model `belongs_to` another, you instruct Rails to maintain Primary Key-Foreign Key information between instances of the two models, and you also get a number of utility methods added to your model. Rails supports six types of associations:
73
73
 
74
74
  * `belongs_to`
75
75
  * `has_one`
@@ -101,13 +101,13 @@ class CreateOrders < ActiveRecord::Migration
101
101
  def change
102
102
  create_table :customers do |t|
103
103
  t.string :name
104
- t.timestamps
104
+ t.timestamps null: false
105
105
  end
106
106
 
107
107
  create_table :orders do |t|
108
- t.belongs_to :customer
108
+ t.belongs_to :customer, index: true
109
109
  t.datetime :order_date
110
- t.timestamps
110
+ t.timestamps null: false
111
111
  end
112
112
  end
113
113
  end
@@ -132,13 +132,13 @@ class CreateSuppliers < ActiveRecord::Migration
132
132
  def change
133
133
  create_table :suppliers do |t|
134
134
  t.string :name
135
- t.timestamps
135
+ t.timestamps null: false
136
136
  end
137
137
 
138
138
  create_table :accounts do |t|
139
- t.belongs_to :supplier
139
+ t.belongs_to :supplier, index: true
140
140
  t.string :account_number
141
- t.timestamps
141
+ t.timestamps null: false
142
142
  end
143
143
  end
144
144
  end
@@ -165,13 +165,13 @@ class CreateCustomers < ActiveRecord::Migration
165
165
  def change
166
166
  create_table :customers do |t|
167
167
  t.string :name
168
- t.timestamps
168
+ t.timestamps null: false
169
169
  end
170
170
 
171
171
  create_table :orders do |t|
172
- t.belongs_to :customer
172
+ t.belongs_to :customer, index: true
173
173
  t.datetime :order_date
174
- t.timestamps
174
+ t.timestamps null: false
175
175
  end
176
176
  end
177
177
  end
@@ -207,19 +207,19 @@ class CreateAppointments < ActiveRecord::Migration
207
207
  def change
208
208
  create_table :physicians do |t|
209
209
  t.string :name
210
- t.timestamps
210
+ t.timestamps null: false
211
211
  end
212
212
 
213
213
  create_table :patients do |t|
214
214
  t.string :name
215
- t.timestamps
215
+ t.timestamps null: false
216
216
  end
217
217
 
218
218
  create_table :appointments do |t|
219
- t.belongs_to :physician
220
- t.belongs_to :patient
219
+ t.belongs_to :physician, index: true
220
+ t.belongs_to :patient, index: true
221
221
  t.datetime :appointment_date
222
- t.timestamps
222
+ t.timestamps null: false
223
223
  end
224
224
  end
225
225
  end
@@ -261,7 +261,10 @@ With `through: :sections` specified, Rails will now understand:
261
261
 
262
262
  ### The `has_one :through` Association
263
263
 
264
- A `has_one :through` association sets up a one-to-one connection with another model. This association indicates that the declaring model can be matched with one instance of another model by proceeding _through_ a third model. For example, if each supplier has one account, and each account is associated with one account history, then the customer model could look like this:
264
+ A `has_one :through` association sets up a one-to-one connection with another model. This association indicates
265
+ that the declaring model can be matched with one instance of another model by proceeding _through_ a third model.
266
+ For example, if each supplier has one account, and each account is associated with one account history, then the
267
+ supplier model could look like this:
265
268
 
266
269
  ```ruby
267
270
  class Supplier < ActiveRecord::Base
@@ -288,19 +291,19 @@ class CreateAccountHistories < ActiveRecord::Migration
288
291
  def change
289
292
  create_table :suppliers do |t|
290
293
  t.string :name
291
- t.timestamps
294
+ t.timestamps null: false
292
295
  end
293
296
 
294
297
  create_table :accounts do |t|
295
- t.belongs_to :supplier
298
+ t.belongs_to :supplier, index: true
296
299
  t.string :account_number
297
- t.timestamps
300
+ t.timestamps null: false
298
301
  end
299
302
 
300
303
  create_table :account_histories do |t|
301
- t.belongs_to :account
304
+ t.belongs_to :account, index: true
302
305
  t.integer :credit_rating
303
- t.timestamps
306
+ t.timestamps null: false
304
307
  end
305
308
  end
306
309
  end
@@ -329,17 +332,17 @@ class CreateAssembliesAndParts < ActiveRecord::Migration
329
332
  def change
330
333
  create_table :assemblies do |t|
331
334
  t.string :name
332
- t.timestamps
335
+ t.timestamps null: false
333
336
  end
334
337
 
335
338
  create_table :parts do |t|
336
339
  t.string :part_number
337
- t.timestamps
340
+ t.timestamps null: false
338
341
  end
339
342
 
340
- create_table :assemblies_parts do |t|
341
- t.belongs_to :assembly
342
- t.belongs_to :part
343
+ create_table :assemblies_parts, id: false do |t|
344
+ t.belongs_to :assembly, index: true
345
+ t.belongs_to :part, index: true
343
346
  end
344
347
  end
345
348
  end
@@ -368,14 +371,16 @@ class CreateSuppliers < ActiveRecord::Migration
368
371
  def change
369
372
  create_table :suppliers do |t|
370
373
  t.string :name
371
- t.timestamps
374
+ t.timestamps null: false
372
375
  end
373
376
 
374
377
  create_table :accounts do |t|
375
378
  t.integer :supplier_id
376
379
  t.string :account_number
377
- t.timestamps
380
+ t.timestamps null: false
378
381
  end
382
+
383
+ add_index :accounts, :supplier_id
379
384
  end
380
385
  end
381
386
  ```
@@ -450,8 +455,10 @@ class CreatePictures < ActiveRecord::Migration
450
455
  t.string :name
451
456
  t.integer :imageable_id
452
457
  t.string :imageable_type
453
- t.timestamps
458
+ t.timestamps null: false
454
459
  end
460
+
461
+ add_index :pictures, :imageable_id
455
462
  end
456
463
  end
457
464
  ```
@@ -463,8 +470,8 @@ class CreatePictures < ActiveRecord::Migration
463
470
  def change
464
471
  create_table :pictures do |t|
465
472
  t.string :name
466
- t.references :imageable, polymorphic: true
467
- t.timestamps
473
+ t.references :imageable, polymorphic: true, index: true
474
+ t.timestamps null: false
468
475
  end
469
476
  end
470
477
  end
@@ -487,6 +494,19 @@ end
487
494
 
488
495
  With this setup, you can retrieve `@employee.subordinates` and `@employee.manager`.
489
496
 
497
+ In your migrations/schema, you will add a references column to the model itself.
498
+
499
+ ```ruby
500
+ class CreateEmployees < ActiveRecord::Migration
501
+ def change
502
+ create_table :employees do |t|
503
+ t.references :manager, index: true
504
+ t.timestamps null: false
505
+ end
506
+ end
507
+ end
508
+ ```
509
+
490
510
  Tips, Tricks, and Warnings
491
511
  --------------------------
492
512
 
@@ -545,6 +565,8 @@ class CreateOrders < ActiveRecord::Migration
545
565
  t.string :order_number
546
566
  t.integer :customer_id
547
567
  end
568
+
569
+ add_index :orders, :customer_id
548
570
  end
549
571
  end
550
572
  ```
@@ -555,7 +577,7 @@ If you create an association some time after you build the underlying model, you
555
577
 
556
578
  If you create a `has_and_belongs_to_many` association, you need to explicitly create the joining table. Unless the name of the join table is explicitly specified by using the `:join_table` option, Active Record creates the name by using the lexical order of the class names. So a join between customer and order models will give the default join table name of "customers_orders" because "c" outranks "o" in lexical ordering.
557
579
 
558
- WARNING: The precedence between model names is calculated using the `<` operator for `String`. This means that if the strings are of different lengths, and the strings are equal when compared up to the shortest length, then the longer string is considered of higher lexical precedence than the shorter one. For example, one would expect the tables "paper\_boxes" and "papers" to generate a join table name of "papers\_paper\_boxes" because of the length of the name "paper\_boxes", but it in fact generates a join table name of "paper\_boxes\_papers" (because the underscore '\_' is lexicographically _less_ than 's' in common encodings).
580
+ WARNING: The precedence between model names is calculated using the `<` operator for `String`. This means that if the strings are of different lengths, and the strings are equal when compared up to the shortest length, then the longer string is considered of higher lexical precedence than the shorter one. For example, one would expect the tables "paper_boxes" and "papers" to generate a join table name of "papers_paper_boxes" because of the length of the name "paper_boxes", but it in fact generates a join table name of "paper_boxes_papers" (because the underscore '_' is lexicographically _less_ than 's' in common encodings).
559
581
 
560
582
  Whatever the name, you must manually generate the join table with an appropriate migration. For example, consider these associations:
561
583
 
@@ -578,6 +600,9 @@ class CreateAssembliesPartsJoinTable < ActiveRecord::Migration
578
600
  t.integer :assembly_id
579
601
  t.integer :part_id
580
602
  end
603
+
604
+ add_index :assemblies_parts, :assembly_id
605
+ add_index :assemblies_parts, :part_id
581
606
  end
582
607
  end
583
608
  ```
@@ -693,6 +718,17 @@ There are a few limitations to `inverse_of` support:
693
718
  * They do not work with `:as` associations.
694
719
  * For `belongs_to` associations, `has_many` inverse associations are ignored.
695
720
 
721
+ Every association will attempt to automatically find the inverse association
722
+ and set the `:inverse_of` option heuristically (based on the association name).
723
+ Most associations with standard names will be supported. However, associations
724
+ that contain the following options will not have their inverses set
725
+ automatically:
726
+
727
+ * :conditions
728
+ * :through
729
+ * :polymorphic
730
+ * :foreign_key
731
+
696
732
  Detailed Association Reference
697
733
  ------------------------------
698
734
 
@@ -704,12 +740,13 @@ The `belongs_to` association creates a one-to-one match with another model. In d
704
740
 
705
741
  #### Methods Added by `belongs_to`
706
742
 
707
- When you declare a `belongs_to` association, the declaring class automatically gains four methods related to the association:
743
+ When you declare a `belongs_to` association, the declaring class automatically gains five methods related to the association:
708
744
 
709
745
  * `association(force_reload = false)`
710
746
  * `association=(associate)`
711
747
  * `build_association(attributes = {})`
712
748
  * `create_association(attributes = {})`
749
+ * `create_association!(attributes = {})`
713
750
 
714
751
  In all of these methods, `association` is replaced with the symbol passed as the first argument to `belongs_to`. For example, given the declaration:
715
752
 
@@ -719,13 +756,14 @@ class Order < ActiveRecord::Base
719
756
  end
720
757
  ```
721
758
 
722
- Each instance of the order model will have these methods:
759
+ Each instance of the `Order` model will have these methods:
723
760
 
724
761
  ```ruby
725
762
  customer
726
763
  customer=
727
764
  build_customer
728
765
  create_customer
766
+ create_customer!
729
767
  ```
730
768
 
731
769
  NOTE: When initializing a new `has_one` or `belongs_to` association you must use the `build_` prefix to build the association, rather than the `association.build` method that would be used for `has_many` or `has_and_belongs_to_many` associations. To create one, use the `create_` prefix.
@@ -766,6 +804,10 @@ The `create_association` method returns a new object of the associated type. Thi
766
804
  customer_name: "John Doe")
767
805
  ```
768
806
 
807
+ ##### `create_association!(attributes = {})`
808
+
809
+ Does the same as `create_association` above, but raises `ActiveRecord::RecordInvalid` if the record is invalid.
810
+
769
811
 
770
812
  #### Options for `belongs_to`
771
813
 
@@ -844,8 +886,12 @@ end
844
886
  Counter cache columns are added to the containing model's list of read-only attributes through `attr_readonly`.
845
887
 
846
888
  ##### `:dependent`
889
+ If you set the `:dependent` option to:
847
890
 
848
- If you set the `:dependent` option to `:destroy`, then deleting this object will call the `destroy` method on the associated object to delete that object. If you set the `:dependent` option to `:delete`, then deleting this object will delete the associated object _without_ calling its `destroy` method. If you set the `:dependent` option to `:restrict`, then attempting to delete this object will result in a `ActiveRecord::DeleteRestrictionError` if there are any associated objects.
891
+ * `:destroy`, when the object is destroyed, `destroy` will be called on its
892
+ associated objects.
893
+ * `:delete`, when the object is destroyed, all its associated objects will be
894
+ deleted directly from the database without calling their `destroy` method.
849
895
 
850
896
  WARNING: You should not specify this option on a `belongs_to` association that is connected with a `has_many` association on the other class. Doing so can lead to orphaned records in your database.
851
897
 
@@ -936,7 +982,7 @@ end
936
982
 
937
983
  ##### `includes`
938
984
 
939
- You can use the `includes` method let you specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:
985
+ You can use the `includes` method to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:
940
986
 
941
987
  ```ruby
942
988
  class LineItem < ActiveRecord::Base
@@ -1002,12 +1048,13 @@ The `has_one` association creates a one-to-one match with another model. In data
1002
1048
 
1003
1049
  #### Methods Added by `has_one`
1004
1050
 
1005
- When you declare a `has_one` association, the declaring class automatically gains four methods related to the association:
1051
+ When you declare a `has_one` association, the declaring class automatically gains five methods related to the association:
1006
1052
 
1007
1053
  * `association(force_reload = false)`
1008
1054
  * `association=(associate)`
1009
1055
  * `build_association(attributes = {})`
1010
1056
  * `create_association(attributes = {})`
1057
+ * `create_association!(attributes = {})`
1011
1058
 
1012
1059
  In all of these methods, `association` is replaced with the symbol passed as the first argument to `has_one`. For example, given the declaration:
1013
1060
 
@@ -1024,6 +1071,7 @@ account
1024
1071
  account=
1025
1072
  build_account
1026
1073
  create_account
1074
+ create_account!
1027
1075
  ```
1028
1076
 
1029
1077
  NOTE: When initializing a new `has_one` or `belongs_to` association you must use the `build_` prefix to build the association, rather than the `association.build` method that would be used for `has_many` or `has_and_belongs_to_many` associations. To create one, use the `create_` prefix.
@@ -1062,6 +1110,10 @@ The `create_association` method returns a new object of the associated type. Thi
1062
1110
  @account = @supplier.create_account(terms: "Net 30")
1063
1111
  ```
1064
1112
 
1113
+ ##### `create_association!(attributes = {})`
1114
+
1115
+ Does the same as `create_association` above, but raises `ActiveRecord::RecordInvalid` if the record is invalid.
1116
+
1065
1117
  #### Options for `has_one`
1066
1118
 
1067
1119
  While Rails uses intelligent defaults that will work well in most situations, there may be times when you want to customize the behavior of the `has_one` association reference. Such customizations can easily be accomplished by passing options when you create the association. For example, this association uses two such options:
@@ -1088,7 +1140,7 @@ The `has_one` association supports these options:
1088
1140
 
1089
1141
  ##### `:as`
1090
1142
 
1091
- Setting the `:as` option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail <a href="#polymorphic-associations">earlier in this guide</a>.
1143
+ Setting the `:as` option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail [earlier in this guide](#polymorphic-associations).
1092
1144
 
1093
1145
  ##### `:autosave`
1094
1146
 
@@ -1114,6 +1166,12 @@ Controls what happens to the associated object when its owner is destroyed:
1114
1166
  * `:restrict_with_exception` causes an exception to be raised if there is an associated record
1115
1167
  * `:restrict_with_error` causes an error to be added to the owner if there is an associated object
1116
1168
 
1169
+ It's necessary not to set or leave `:nullify` option for those associations
1170
+ that have `NOT NULL` database constraints. If you don't set `dependent` to
1171
+ destroy such associations you won't be able to change the associated object
1172
+ because initial associated object foreign key will be set to unallowed `NULL`
1173
+ value.
1174
+
1117
1175
  ##### `:foreign_key`
1118
1176
 
1119
1177
  By convention, Rails assumes that the column used to hold the foreign key on the other model is the name of this model with the suffix `_id` added. The `:foreign_key` option lets you set the name of the foreign key directly:
@@ -1154,7 +1212,7 @@ The `:source_type` option specifies the source association type for a `has_one :
1154
1212
 
1155
1213
  ##### `:through`
1156
1214
 
1157
- The `:through` option specifies a join model through which to perform the query. `has_one :through` associations were discussed in detail <a href="#the-has-one-through-association">earlier in this guide</a>.
1215
+ The `:through` option specifies a join model through which to perform the query. `has_one :through` associations were discussed in detail [earlier in this guide](#the-has-one-through-association).
1158
1216
 
1159
1217
  ##### `:validate`
1160
1218
 
@@ -1257,15 +1315,15 @@ The `has_many` association creates a one-to-many relationship with another model
1257
1315
 
1258
1316
  #### Methods Added by `has_many`
1259
1317
 
1260
- When you declare a `has_many` association, the declaring class automatically gains 13 methods related to the association:
1318
+ When you declare a `has_many` association, the declaring class automatically gains 16 methods related to the association:
1261
1319
 
1262
1320
  * `collection(force_reload = false)`
1263
1321
  * `collection<<(object, ...)`
1264
1322
  * `collection.delete(object, ...)`
1265
1323
  * `collection.destroy(object, ...)`
1266
- * `collection=objects`
1324
+ * `collection=(objects)`
1267
1325
  * `collection_singular_ids`
1268
- * `collection_singular_ids=ids`
1326
+ * `collection_singular_ids=(ids)`
1269
1327
  * `collection.clear`
1270
1328
  * `collection.empty?`
1271
1329
  * `collection.size`
@@ -1274,6 +1332,7 @@ When you declare a `has_many` association, the declaring class automatically gai
1274
1332
  * `collection.exists?(...)`
1275
1333
  * `collection.build(attributes = {}, ...)`
1276
1334
  * `collection.create(attributes = {})`
1335
+ * `collection.create!(attributes = {})`
1277
1336
 
1278
1337
  In all of these methods, `collection` is replaced with the symbol passed as the first argument to `has_many`, and `collection_singular` is replaced with the singularized version of that symbol. For example, given the declaration:
1279
1338
 
@@ -1283,16 +1342,16 @@ class Customer < ActiveRecord::Base
1283
1342
  end
1284
1343
  ```
1285
1344
 
1286
- Each instance of the customer model will have these methods:
1345
+ Each instance of the `Customer` model will have these methods:
1287
1346
 
1288
1347
  ```ruby
1289
1348
  orders(force_reload = false)
1290
1349
  orders<<(object, ...)
1291
1350
  orders.delete(object, ...)
1292
1351
  orders.destroy(object, ...)
1293
- orders=objects
1352
+ orders=(objects)
1294
1353
  order_ids
1295
- order_ids=ids
1354
+ order_ids=(ids)
1296
1355
  orders.clear
1297
1356
  orders.empty?
1298
1357
  orders.size
@@ -1301,6 +1360,7 @@ orders.where(...)
1301
1360
  orders.exists?(...)
1302
1361
  orders.build(attributes = {}, ...)
1303
1362
  orders.create(attributes = {})
1363
+ orders.create!(attributes = {})
1304
1364
  ```
1305
1365
 
1306
1366
  ##### `collection(force_reload = false)`
@@ -1339,7 +1399,7 @@ The `collection.destroy` method removes one or more objects from the collection
1339
1399
 
1340
1400
  WARNING: Objects will _always_ be removed from the database, ignoring the `:dependent` option.
1341
1401
 
1342
- ##### `collection=objects`
1402
+ ##### `collection=(objects)`
1343
1403
 
1344
1404
  The `collection=` method makes the collection contain only the supplied objects, by adding and deleting as appropriate.
1345
1405
 
@@ -1351,13 +1411,19 @@ The `collection_singular_ids` method returns an array of the ids of the objects
1351
1411
  @order_ids = @customer.order_ids
1352
1412
  ```
1353
1413
 
1354
- ##### `collection_singular_ids=ids`
1414
+ ##### `collection_singular_ids=(ids)`
1355
1415
 
1356
1416
  The `collection_singular_ids=` method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.
1357
1417
 
1358
1418
  ##### `collection.clear`
1359
1419
 
1360
- The `collection.clear` method removes every object from the collection. This destroys the associated objects if they are associated with `dependent: :destroy`, deletes them directly from the database if `dependent: :delete_all`, and otherwise sets their foreign keys to `NULL`.
1420
+ The `collection.clear` method removes all objects from the collection according to the strategy specified by the `dependent` option. If no option is given, it follows the default strategy. The default strategy for `has_many :through` associations is `delete_all`, and for `has_many` associations is to set the foreign keys to `NULL`.
1421
+
1422
+ ```ruby
1423
+ @customer.orders.clear
1424
+ ```
1425
+
1426
+ WARNING: Objects will be delete if they're associated with `dependent: :destroy`, just like `dependent: :delete_all`.
1361
1427
 
1362
1428
  ##### `collection.empty?`
1363
1429
 
@@ -1396,7 +1462,9 @@ The `collection.where` method finds objects within the collection based on the c
1396
1462
 
1397
1463
  ##### `collection.exists?(...)`
1398
1464
 
1399
- The `collection.exists?` method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as `ActiveRecord::Base.exists?`.
1465
+ The `collection.exists?` method checks whether an object meeting the supplied
1466
+ conditions exists in the collection. It uses the same syntax and options as
1467
+ [`ActiveRecord::Base.exists?`](http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-exists-3F).
1400
1468
 
1401
1469
  ##### `collection.build(attributes = {}, ...)`
1402
1470
 
@@ -1416,6 +1484,10 @@ The `collection.create` method returns a new object of the associated type. This
1416
1484
  order_number: "A12345")
1417
1485
  ```
1418
1486
 
1487
+ ##### `collection.create!(attributes = {})`
1488
+
1489
+ Does the same as `collection.create` above, but raises `ActiveRecord::RecordInvalid` if the record is invalid.
1490
+
1419
1491
  #### Options for `has_many`
1420
1492
 
1421
1493
  While Rails uses intelligent defaults that will work well in most situations, there may be times when you want to customize the behavior of the `has_many` association reference. Such customizations can easily be accomplished by passing options when you create the association. For example, this association uses two such options:
@@ -1442,7 +1514,7 @@ The `has_many` association supports these options:
1442
1514
 
1443
1515
  ##### `:as`
1444
1516
 
1445
- Setting the `:as` option indicates that this is a polymorphic association, as discussed <a href="#polymorphic-associations">earlier in this guide</a>.
1517
+ Setting the `:as` option indicates that this is a polymorphic association, as discussed [earlier in this guide](#polymorphic-associations).
1446
1518
 
1447
1519
  ##### `:autosave`
1448
1520
 
@@ -1468,8 +1540,6 @@ Controls what happens to the associated objects when their owner is destroyed:
1468
1540
  * `:restrict_with_exception` causes an exception to be raised if there are any associated records
1469
1541
  * `:restrict_with_error` causes an error to be added to the owner if there are any associated objects
1470
1542
 
1471
- NOTE: This option is ignored when you use the `:through` option on the association.
1472
-
1473
1543
  ##### `:foreign_key`
1474
1544
 
1475
1545
  By convention, Rails assumes that the column used to hold the foreign key on the other model is the name of this model with the suffix `_id` added. The `:foreign_key` option lets you set the name of the foreign key directly:
@@ -1500,6 +1570,20 @@ end
1500
1570
 
1501
1571
  By convention, Rails assumes that the column used to hold the primary key of the association is `id`. You can override this and explicitly specify the primary key with the `:primary_key` option.
1502
1572
 
1573
+ Let's say that `users` table has `id` as the primary_key but it also has
1574
+ `guid` column. And the requirement is that `todos` table should hold
1575
+ `guid` column value and not `id` value. This can be achieved like this
1576
+
1577
+ ```ruby
1578
+ class User < ActiveRecord::Base
1579
+ has_many :todos, primary_key: :guid
1580
+ end
1581
+ ```
1582
+
1583
+ Now if we execute `@user.todos.create` then `@todo` record will have
1584
+ `user_id` value as the `guid` value of `@user`.
1585
+
1586
+
1503
1587
  ##### `:source`
1504
1588
 
1505
1589
  The `:source` option specifies the source association name for a `has_many :through` association. You only need to use this option if the name of the source association cannot be automatically inferred from the association name.
@@ -1510,7 +1594,7 @@ The `:source_type` option specifies the source association type for a `has_many
1510
1594
 
1511
1595
  ##### `:through`
1512
1596
 
1513
- The `:through` option specifies a join model through which to perform the query. `has_many :through` associations provide a way to implement many-to-many relationships, as discussed <a href="#the-has-many-through-association">earlier in this guide</a>.
1597
+ The `:through` option specifies a join model through which to perform the query. `has_many :through` associations provide a way to implement many-to-many relationships, as discussed [earlier in this guide](#the-has-many-through-association).
1514
1598
 
1515
1599
  ##### `:validate`
1516
1600
 
@@ -1563,7 +1647,7 @@ If you use a hash-style `where` option, then record creation via this associatio
1563
1647
 
1564
1648
  ##### `extending`
1565
1649
 
1566
- The `extending` method specifies a named module to extend the association proxy. Association extensions are discussed in detail <a href="#association-extensions">later in this guide</a>.
1650
+ The `extending` method specifies a named module to extend the association proxy. Association extensions are discussed in detail [later in this guide](#association-extensions).
1567
1651
 
1568
1652
  ##### `group`
1569
1653
 
@@ -1656,58 +1740,58 @@ mostly useful together with the `:through` option.
1656
1740
  ```ruby
1657
1741
  class Person < ActiveRecord::Base
1658
1742
  has_many :readings
1659
- has_many :posts, through: :readings
1743
+ has_many :articles, through: :readings
1660
1744
  end
1661
1745
 
1662
1746
  person = Person.create(name: 'John')
1663
- post = Post.create(name: 'a1')
1664
- person.posts << post
1665
- person.posts << post
1666
- person.posts.inspect # => [#<Post id: 5, name: "a1">, #<Post id: 5, name: "a1">]
1667
- Reading.all.inspect # => [#<Reading id: 12, person_id: 5, post_id: 5>, #<Reading id: 13, person_id: 5, post_id: 5>]
1747
+ article = Article.create(name: 'a1')
1748
+ person.articles << article
1749
+ person.articles << article
1750
+ person.articles.inspect # => [#<Article id: 5, name: "a1">, #<Article id: 5, name: "a1">]
1751
+ Reading.all.inspect # => [#<Reading id: 12, person_id: 5, article_id: 5>, #<Reading id: 13, person_id: 5, article_id: 5>]
1668
1752
  ```
1669
1753
 
1670
- In the above case there are two readings and `person.posts` brings out both of
1671
- them even though these records are pointing to the same post.
1754
+ In the above case there are two readings and `person.articles` brings out both of
1755
+ them even though these records are pointing to the same article.
1672
1756
 
1673
1757
  Now let's set `distinct`:
1674
1758
 
1675
1759
  ```ruby
1676
1760
  class Person
1677
1761
  has_many :readings
1678
- has_many :posts, -> { distinct }, through: :readings
1762
+ has_many :articles, -> { distinct }, through: :readings
1679
1763
  end
1680
1764
 
1681
1765
  person = Person.create(name: 'Honda')
1682
- post = Post.create(name: 'a1')
1683
- person.posts << post
1684
- person.posts << post
1685
- person.posts.inspect # => [#<Post id: 7, name: "a1">]
1686
- Reading.all.inspect # => [#<Reading id: 16, person_id: 7, post_id: 7>, #<Reading id: 17, person_id: 7, post_id: 7>]
1766
+ article = Article.create(name: 'a1')
1767
+ person.articles << article
1768
+ person.articles << article
1769
+ person.articles.inspect # => [#<Article id: 7, name: "a1">]
1770
+ Reading.all.inspect # => [#<Reading id: 16, person_id: 7, article_id: 7>, #<Reading id: 17, person_id: 7, article_id: 7>]
1687
1771
  ```
1688
1772
 
1689
- In the above case there are still two readings. However `person.posts` shows
1690
- only one post because the collection loads only unique records.
1773
+ In the above case there are still two readings. However `person.articles` shows
1774
+ only one article because the collection loads only unique records.
1691
1775
 
1692
- If you want to make sure that, upon insertion, all of the records in the
1693
- persisted association are distinct (so that you can be sure that when you
1694
- inspect the association that you will never find duplicate records), you should
1695
- add a unique index on the table itself. For example, if you have a table named
1696
- ``person_posts`` and you want to make sure all the posts are unique, you could
1776
+ If you want to make sure that, upon insertion, all of the records in the
1777
+ persisted association are distinct (so that you can be sure that when you
1778
+ inspect the association that you will never find duplicate records), you should
1779
+ add a unique index on the table itself. For example, if you have a table named
1780
+ `person_articles` and you want to make sure all the articles are unique, you could
1697
1781
  add the following in a migration:
1698
1782
 
1699
1783
  ```ruby
1700
- add_index :person_posts, :post, :unique => true
1784
+ add_index :person_articles, :article, unique: true
1701
1785
  ```
1702
1786
 
1703
- Note that checking for uniqueness using something like ``include?`` is subject
1704
- to race conditions. Do not attempt to use ``include?`` to enforce distinctness
1705
- in an association. For instance, using the post example from above, the
1706
- following code would be racy because multiple users could be attempting this
1787
+ Note that checking for uniqueness using something like `include?` is subject
1788
+ to race conditions. Do not attempt to use `include?` to enforce distinctness
1789
+ in an association. For instance, using the article example from above, the
1790
+ following code would be racy because multiple users could be attempting this
1707
1791
  at the same time:
1708
1792
 
1709
1793
  ```ruby
1710
- person.posts << post unless person.posts.include?(post)
1794
+ person.articles << article unless person.articles.include?(article)
1711
1795
  ```
1712
1796
 
1713
1797
  #### When are Objects Saved?
@@ -1726,15 +1810,15 @@ The `has_and_belongs_to_many` association creates a many-to-many relationship wi
1726
1810
 
1727
1811
  #### Methods Added by `has_and_belongs_to_many`
1728
1812
 
1729
- When you declare a `has_and_belongs_to_many` association, the declaring class automatically gains 13 methods related to the association:
1813
+ When you declare a `has_and_belongs_to_many` association, the declaring class automatically gains 16 methods related to the association:
1730
1814
 
1731
1815
  * `collection(force_reload = false)`
1732
1816
  * `collection<<(object, ...)`
1733
1817
  * `collection.delete(object, ...)`
1734
1818
  * `collection.destroy(object, ...)`
1735
- * `collection=objects`
1819
+ * `collection=(objects)`
1736
1820
  * `collection_singular_ids`
1737
- * `collection_singular_ids=ids`
1821
+ * `collection_singular_ids=(ids)`
1738
1822
  * `collection.clear`
1739
1823
  * `collection.empty?`
1740
1824
  * `collection.size`
@@ -1743,6 +1827,7 @@ When you declare a `has_and_belongs_to_many` association, the declaring class au
1743
1827
  * `collection.exists?(...)`
1744
1828
  * `collection.build(attributes = {})`
1745
1829
  * `collection.create(attributes = {})`
1830
+ * `collection.create!(attributes = {})`
1746
1831
 
1747
1832
  In all of these methods, `collection` is replaced with the symbol passed as the first argument to `has_and_belongs_to_many`, and `collection_singular` is replaced with the singularized version of that symbol. For example, given the declaration:
1748
1833
 
@@ -1752,16 +1837,16 @@ class Part < ActiveRecord::Base
1752
1837
  end
1753
1838
  ```
1754
1839
 
1755
- Each instance of the part model will have these methods:
1840
+ Each instance of the `Part` model will have these methods:
1756
1841
 
1757
1842
  ```ruby
1758
1843
  assemblies(force_reload = false)
1759
1844
  assemblies<<(object, ...)
1760
1845
  assemblies.delete(object, ...)
1761
1846
  assemblies.destroy(object, ...)
1762
- assemblies=objects
1847
+ assemblies=(objects)
1763
1848
  assembly_ids
1764
- assembly_ids=ids
1849
+ assembly_ids=(ids)
1765
1850
  assemblies.clear
1766
1851
  assemblies.empty?
1767
1852
  assemblies.size
@@ -1770,6 +1855,7 @@ assemblies.where(...)
1770
1855
  assemblies.exists?(...)
1771
1856
  assemblies.build(attributes = {}, ...)
1772
1857
  assemblies.create(attributes = {})
1858
+ assemblies.create!(attributes = {})
1773
1859
  ```
1774
1860
 
1775
1861
  ##### Additional Column Methods
@@ -1815,7 +1901,7 @@ The `collection.destroy` method removes one or more objects from the collection
1815
1901
  @part.assemblies.destroy(@assembly1)
1816
1902
  ```
1817
1903
 
1818
- ##### `collection=objects`
1904
+ ##### `collection=(objects)`
1819
1905
 
1820
1906
  The `collection=` method makes the collection contain only the supplied objects, by adding and deleting as appropriate.
1821
1907
 
@@ -1827,7 +1913,7 @@ The `collection_singular_ids` method returns an array of the ids of the objects
1827
1913
  @assembly_ids = @part.assembly_ids
1828
1914
  ```
1829
1915
 
1830
- ##### `collection_singular_ids=ids`
1916
+ ##### `collection_singular_ids=(ids)`
1831
1917
 
1832
1918
  The `collection_singular_ids=` method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.
1833
1919
 
@@ -1871,7 +1957,9 @@ The `collection.where` method finds objects within the collection based on the c
1871
1957
 
1872
1958
  ##### `collection.exists?(...)`
1873
1959
 
1874
- The `collection.exists?` method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as `ActiveRecord::Base.exists?`.
1960
+ The `collection.exists?` method checks whether an object meeting the supplied
1961
+ conditions exists in the collection. It uses the same syntax and options as
1962
+ [`ActiveRecord::Base.exists?`](http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-exists-3F).
1875
1963
 
1876
1964
  ##### `collection.build(attributes = {})`
1877
1965
 
@@ -1889,14 +1977,18 @@ The `collection.create` method returns a new object of the associated type. This
1889
1977
  @assembly = @part.assemblies.create({assembly_name: "Transmission housing"})
1890
1978
  ```
1891
1979
 
1980
+ ##### `collection.create!(attributes = {})`
1981
+
1982
+ Does the same as `collection.create`, but raises `ActiveRecord::RecordInvalid` if the record is invalid.
1983
+
1892
1984
  #### Options for `has_and_belongs_to_many`
1893
1985
 
1894
1986
  While Rails uses intelligent defaults that will work well in most situations, there may be times when you want to customize the behavior of the `has_and_belongs_to_many` association reference. Such customizations can easily be accomplished by passing options when you create the association. For example, this association uses two such options:
1895
1987
 
1896
1988
  ```ruby
1897
1989
  class Parts < ActiveRecord::Base
1898
- has_and_belongs_to_many :assemblies, uniq: true,
1899
- read_only: true
1990
+ has_and_belongs_to_many :assemblies, -> { readonly },
1991
+ autosave: true
1900
1992
  end
1901
1993
  ```
1902
1994
 
@@ -1917,7 +2009,7 @@ TIP: The `:foreign_key` and `:association_foreign_key` options are useful when s
1917
2009
 
1918
2010
  ```ruby
1919
2011
  class User < ActiveRecord::Base
1920
- has_and_belongs_to_many :friends,
2012
+ has_and_belongs_to_many :friends,
1921
2013
  class_name: "User",
1922
2014
  foreign_key: "this_user_id",
1923
2015
  association_foreign_key: "other_user_id"
@@ -2006,7 +2098,7 @@ If you use a hash-style `where`, then record creation via this association will
2006
2098
 
2007
2099
  ##### `extending`
2008
2100
 
2009
- The `extending` method specifies a named module to extend the association proxy. Association extensions are discussed in detail <a href="#association-extensions">later in this guide</a>.
2101
+ The `extending` method specifies a named module to extend the association proxy. Association extensions are discussed in detail [later in this guide](#association-extensions).
2010
2102
 
2011
2103
  ##### `group`
2012
2104
 
@@ -2122,7 +2214,7 @@ You're not limited to the functionality that Rails automatically builds into ass
2122
2214
  class Customer < ActiveRecord::Base
2123
2215
  has_many :orders do
2124
2216
  def find_by_order_prefix(order_number)
2125
- find_by_region_id(order_number[0..2])
2217
+ find_by(region_id: order_number[0..2])
2126
2218
  end
2127
2219
  end
2128
2220
  end