railties 3.0.20 → 3.1.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (193) hide show
  1. data/CHANGELOG +36 -49
  2. data/README.rdoc +2 -1
  3. data/guides/assets/stylesheets/fixes.css +16 -0
  4. data/guides/rails_guides.rb +2 -2
  5. data/guides/rails_guides/generator.rb +8 -3
  6. data/guides/rails_guides/textile_extensions.rb +4 -2
  7. data/guides/source/2_2_release_notes.textile +3 -3
  8. data/guides/source/2_3_release_notes.textile +2 -2
  9. data/guides/source/3_0_release_notes.textile +14 -14
  10. data/guides/source/action_controller_overview.textile +54 -79
  11. data/guides/source/action_mailer_basics.textile +39 -9
  12. data/guides/source/action_view_overview.textile +257 -211
  13. data/guides/source/active_record_basics.textile +1 -1
  14. data/guides/source/active_record_querying.textile +217 -27
  15. data/guides/source/active_record_validations_callbacks.textile +94 -25
  16. data/guides/source/active_support_core_extensions.textile +109 -77
  17. data/guides/source/ajax_on_rails.textile +15 -150
  18. data/guides/source/api_documentation_guidelines.textile +12 -12
  19. data/guides/source/association_basics.textile +74 -60
  20. data/guides/source/caching_with_rails.textile +59 -60
  21. data/guides/source/command_line.textile +46 -47
  22. data/guides/source/configuring.textile +55 -37
  23. data/guides/source/contribute.textile +7 -7
  24. data/guides/source/contributing_to_ruby_on_rails.textile +14 -23
  25. data/guides/source/credits.html.erb +3 -3
  26. data/guides/source/debugging_rails_applications.textile +59 -46
  27. data/guides/source/form_helpers.textile +76 -31
  28. data/guides/source/generators.textile +39 -40
  29. data/guides/source/getting_started.textile +73 -94
  30. data/guides/source/i18n.textile +64 -58
  31. data/guides/source/index.html.erb +3 -3
  32. data/guides/source/initialization.textile +634 -3284
  33. data/guides/source/layout.html.erb +6 -7
  34. data/guides/source/layouts_and_rendering.textile +59 -60
  35. data/guides/source/migrations.textile +63 -59
  36. data/guides/source/nested_model_forms.textile +2 -2
  37. data/guides/source/performance_testing.textile +16 -16
  38. data/guides/source/plugins.textile +236 -1280
  39. data/guides/source/rails_application_templates.textile +37 -29
  40. data/guides/source/rails_on_rack.textile +4 -9
  41. data/guides/source/routing.textile +96 -75
  42. data/guides/source/ruby_on_rails_guides_guidelines.textile +19 -12
  43. data/guides/source/security.textile +57 -30
  44. data/guides/source/testing.textile +26 -24
  45. data/guides/w3c_validator.rb +2 -2
  46. data/lib/rails.rb +1 -7
  47. data/lib/rails/application.rb +46 -76
  48. data/lib/rails/application/bootstrap.rb +6 -11
  49. data/lib/rails/application/configuration.rb +43 -40
  50. data/lib/rails/application/finisher.rb +16 -4
  51. data/lib/rails/application/railties.rb +6 -24
  52. data/lib/rails/application/routes_reloader.rb +45 -0
  53. data/lib/rails/backtrace_cleaner.rb +1 -1
  54. data/lib/rails/cli.rb +7 -5
  55. data/lib/rails/commands.rb +27 -2
  56. data/lib/rails/commands/application.rb +14 -1
  57. data/lib/rails/commands/benchmarker.rb +3 -1
  58. data/lib/rails/commands/dbconsole.rb +2 -2
  59. data/lib/rails/commands/destroy.rb +3 -1
  60. data/lib/rails/commands/generate.rb +3 -1
  61. data/lib/rails/commands/plugin.rb +2 -7
  62. data/lib/rails/commands/plugin_new.rb +10 -0
  63. data/lib/rails/commands/profiler.rb +3 -1
  64. data/lib/rails/commands/server.rb +4 -0
  65. data/lib/rails/configuration.rb +8 -81
  66. data/lib/rails/console/app.rb +2 -2
  67. data/lib/rails/engine.rb +460 -78
  68. data/lib/rails/engine/configuration.rb +46 -49
  69. data/lib/rails/engine/railties.rb +33 -0
  70. data/lib/rails/generators.rb +11 -5
  71. data/lib/rails/generators/actions.rb +2 -27
  72. data/lib/rails/generators/app_base.rb +216 -0
  73. data/lib/rails/generators/base.rb +3 -2
  74. data/lib/rails/generators/erb/scaffold/templates/index.html.erb +1 -1
  75. data/lib/rails/generators/generated_attribute.rb +2 -1
  76. data/lib/rails/generators/migration.rb +6 -2
  77. data/lib/rails/generators/named_base.rb +79 -3
  78. data/lib/rails/generators/rails/app/app_generator.rb +44 -209
  79. data/lib/rails/generators/rails/app/templates/Gemfile +15 -31
  80. data/lib/rails/generators/rails/app/templates/README +2 -2
  81. data/lib/rails/generators/rails/app/templates/Rakefile +1 -1
  82. data/lib/rails/generators/rails/app/templates/{public → app/assets}/images/rails.png +0 -0
  83. data/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt +8 -0
  84. data/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css +5 -0
  85. data/lib/rails/generators/rails/app/templates/app/mailers/.empty_directory +0 -0
  86. data/lib/rails/generators/rails/app/templates/app/models/.empty_directory +0 -0
  87. data/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +4 -4
  88. data/lib/rails/generators/rails/app/templates/config/application.rb +19 -3
  89. data/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml +4 -4
  90. data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml +11 -6
  91. data/lib/rails/generators/rails/app/templates/config/databases/jdbcsqlite3.yml +3 -3
  92. data/lib/rails/generators/rails/app/templates/config/databases/oracle.yml +1 -1
  93. data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +1 -2
  94. data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +14 -11
  95. data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +5 -1
  96. data/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt +1 -1
  97. data/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt +12 -0
  98. data/lib/rails/generators/rails/app/templates/config/locales/en.yml +1 -1
  99. data/lib/rails/generators/rails/app/templates/config/routes.rb +1 -1
  100. data/lib/rails/generators/rails/app/templates/db/{seeds.rb → seeds.rb.tt} +2 -2
  101. data/lib/rails/generators/rails/app/templates/public/index.html +10 -8
  102. data/lib/rails/generators/rails/app/templates/public/stylesheets/.empty_directory +0 -0
  103. data/lib/rails/generators/rails/app/templates/test/fixtures/.empty_directory +0 -0
  104. data/lib/rails/generators/rails/app/templates/test/functional/.empty_directory +0 -0
  105. data/lib/rails/generators/rails/app/templates/test/integration/.empty_directory +0 -0
  106. data/lib/rails/generators/rails/app/templates/test/{test_helper.rb.tt → test_helper.rb} +0 -0
  107. data/lib/rails/generators/rails/app/templates/test/unit/.empty_directory +0 -0
  108. data/lib/rails/generators/rails/assets/USAGE +20 -0
  109. data/lib/rails/generators/rails/assets/assets_generator.rb +39 -0
  110. data/lib/rails/generators/rails/assets/templates/javascript.js +2 -0
  111. data/lib/rails/generators/rails/assets/templates/javascript.js.coffee +3 -0
  112. data/lib/rails/generators/rails/assets/templates/stylesheet.css +4 -0
  113. data/lib/rails/generators/rails/assets/templates/stylesheet.css.scss +5 -0
  114. data/lib/rails/generators/rails/controller/controller_generator.rb +1 -1
  115. data/lib/rails/generators/rails/controller/templates/controller.rb +2 -0
  116. data/lib/rails/generators/rails/generator/generator_generator.rb +2 -2
  117. data/lib/rails/generators/rails/generator/templates/templates/.empty_directory +0 -0
  118. data/lib/rails/generators/rails/helper/templates/helper.rb +2 -0
  119. data/lib/rails/generators/rails/plugin/plugin_generator.rb +7 -0
  120. data/lib/rails/generators/rails/plugin/templates/Rakefile.tt +4 -4
  121. data/lib/rails/generators/rails/plugin_new/USAGE +10 -0
  122. data/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb +303 -0
  123. data/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec +9 -0
  124. data/lib/rails/generators/rails/plugin_new/templates/Gemfile +11 -0
  125. data/lib/rails/generators/rails/plugin_new/templates/MIT-LICENSE +20 -0
  126. data/lib/rails/generators/rails/plugin_new/templates/README.rdoc +3 -0
  127. data/lib/rails/generators/rails/plugin_new/templates/Rakefile +21 -0
  128. data/lib/rails/generators/rails/plugin_new/templates/app/controllers/%name%/application_controller.rb.tt +4 -0
  129. data/lib/rails/generators/rails/plugin_new/templates/app/helpers/%name%/application_helper.rb.tt +4 -0
  130. data/lib/rails/generators/rails/plugin_new/templates/app/models/.empty_directory +0 -0
  131. data/lib/rails/generators/rails/plugin_new/templates/config/routes.rb +6 -0
  132. data/lib/rails/generators/rails/plugin_new/templates/gitignore +6 -0
  133. data/lib/rails/generators/rails/plugin_new/templates/lib/%name%.rb +6 -0
  134. data/lib/rails/generators/rails/plugin_new/templates/lib/%name%/engine.rb +7 -0
  135. data/lib/rails/generators/rails/plugin_new/templates/lib/tasks/%name%_tasks.rake +4 -0
  136. data/lib/rails/generators/rails/plugin_new/templates/rails/application.rb +16 -0
  137. data/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb +10 -0
  138. data/lib/rails/generators/rails/plugin_new/templates/rails/routes.rb +4 -0
  139. data/lib/rails/generators/rails/plugin_new/templates/script/rails.tt +5 -0
  140. data/lib/rails/generators/rails/plugin_new/templates/test/%name%_test.rb +7 -0
  141. data/lib/rails/generators/rails/plugin_new/templates/test/integration/navigation_test.rb +12 -0
  142. data/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb +10 -0
  143. data/lib/rails/generators/rails/resource/resource_generator.rb +2 -2
  144. data/lib/rails/generators/rails/scaffold/scaffold_generator.rb +20 -1
  145. data/lib/rails/generators/rails/{stylesheets → scaffold}/templates/scaffold.css +0 -0
  146. data/lib/rails/generators/rails/scaffold/templates/scaffold.css.scss +58 -0
  147. data/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +21 -19
  148. data/lib/rails/generators/resource_helpers.rb +3 -3
  149. data/lib/rails/generators/test_case.rb +2 -20
  150. data/lib/rails/generators/test_unit/controller/templates/functional_test.rb +5 -4
  151. data/lib/rails/generators/test_unit/helper/templates/helper_test.rb +2 -0
  152. data/lib/rails/generators/test_unit/integration/templates/integration_test.rb +3 -4
  153. data/lib/rails/generators/test_unit/mailer/templates/functional_test.rb +5 -4
  154. data/lib/rails/generators/test_unit/model/templates/fixtures.yml +1 -1
  155. data/lib/rails/generators/test_unit/model/templates/unit_test.rb +5 -4
  156. data/lib/rails/generators/test_unit/observer/templates/unit_test.rb +5 -4
  157. data/lib/rails/generators/test_unit/plugin/templates/%file_name%_test.rb.tt +3 -4
  158. data/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +7 -5
  159. data/lib/rails/info.rb +0 -1
  160. data/lib/rails/paths.rb +119 -65
  161. data/lib/rails/plugin.rb +18 -19
  162. data/lib/rails/rack/log_tailer.rb +1 -1
  163. data/lib/rails/railtie.rb +50 -47
  164. data/lib/rails/railtie/configurable.rb +20 -10
  165. data/lib/rails/railtie/configuration.rb +20 -19
  166. data/lib/rails/source_annotation_extractor.rb +5 -5
  167. data/lib/rails/tasks.rb +1 -0
  168. data/lib/rails/tasks/assets.rake +10 -0
  169. data/lib/rails/tasks/documentation.rake +2 -8
  170. data/lib/rails/tasks/engine.rake +69 -0
  171. data/lib/rails/tasks/framework.rake +4 -21
  172. data/lib/rails/tasks/misc.rake +1 -1
  173. data/lib/rails/tasks/routes.rake +2 -1
  174. data/lib/rails/test_help.rb +17 -1
  175. data/lib/rails/test_unit/railtie.rb +1 -1
  176. data/lib/rails/test_unit/testing.rake +8 -3
  177. data/lib/rails/version.rb +3 -3
  178. metadata +128 -100
  179. checksums.yaml +0 -7
  180. data/lib/rails/application/configurable.rb +0 -19
  181. data/lib/rails/console/sandbox.rb +0 -6
  182. data/lib/rails/deprecation.rb +0 -41
  183. data/lib/rails/engine/configurable.rb +0 -25
  184. data/lib/rails/generators/rails/app/templates/config/databases/jdbc.yml +0 -62
  185. data/lib/rails/generators/rails/app/templates/public/javascripts/application.js +0 -2
  186. data/lib/rails/generators/rails/app/templates/public/javascripts/controls.js +0 -965
  187. data/lib/rails/generators/rails/app/templates/public/javascripts/dragdrop.js +0 -974
  188. data/lib/rails/generators/rails/app/templates/public/javascripts/effects.js +0 -1123
  189. data/lib/rails/generators/rails/app/templates/public/javascripts/prototype.js +0 -6001
  190. data/lib/rails/generators/rails/app/templates/public/javascripts/rails.js +0 -202
  191. data/lib/rails/generators/rails/stylesheets/USAGE +0 -5
  192. data/lib/rails/generators/rails/stylesheets/stylesheets_generator.rb +0 -9
  193. data/lib/rails/info_routes.rb +0 -3
@@ -27,9 +27,11 @@ Communicate to the reader the current way of doing things, both explicitly and i
27
27
 
28
28
  Documentation has to be concise but comprehensive. Explore and document edge cases. What happens if a module is anonymous? What if a collection is empty? What if an argument is nil?
29
29
 
30
- The proper names of Rails components have a space in between the words, like "Active Support". +ActiveRecord+ is a Ruby module, whereas Active Record is an ORM. All Rails documentation should consistently refer to Rails components by their proper name, and if in your next blog post or presentation you remember this tidbit and take it into account that'd be fenomenal :).
30
+ The proper names of Rails components have a space in between the words, like "Active Support". +ActiveRecord+ is a Ruby module, whereas Active Record is an ORM. All Rails documentation should consistently refer to Rails components by their proper name, and if in your next blog post or presentation you remember this tidbit and take it into account that'd be phenomenal.
31
31
 
32
- Spell names correctly: HTML, MySQL, JavaScript, ERb. Use the article "an" for "SQL", as in "an SQL statement". Also "an SQLite database".
32
+ Spell names correctly: Arel, Test::Unit, RSpec, HTML, MySQL, JavaScript, ERB. When in doubt, please have a look at some authoritative source like their official documentation.
33
+
34
+ Use the article "an" for "SQL", as in "an SQL statement". Also "an SQLite database".
33
35
 
34
36
  h3. Example Code
35
37
 
@@ -44,10 +46,10 @@ Short docs do not need an explicit "Examples" label to introduce snippets, they
44
46
  # Converts a collection of elements into a formatted string by calling
45
47
  # <tt>to_s</tt> on all elements and joining them.
46
48
  #
47
- # Blog.find(:all).to_formatted_s # => "First PostSecond PostThird Post"
49
+ # Blog.all.to_formatted_s # => "First PostSecond PostThird Post"
48
50
  </ruby>
49
51
 
50
- On the other hand big chunks of structured documentation may have a separate "Examples" section:
52
+ On the other hand, big chunks of structured documentation may have a separate "Examples" section:
51
53
 
52
54
  <ruby>
53
55
  # ==== Examples
@@ -114,14 +116,12 @@ Use fixed-width fonts for:
114
116
  * file names
115
117
 
116
118
  <ruby>
117
- # Copies the instance variables of +object+ into +self+.
118
- #
119
- # Instance variable names in the +exclude+ array are ignored. If +object+
120
- # responds to <tt>protected_instance_variables</tt> the ones returned are
121
- # also ignored. For example, Rails controllers implement that method.
122
- # ...
123
- def copy_instance_variables_from(object, exclude = [])
124
- ...
119
+ class Array
120
+ # Calls <tt>to_param</tt> on all its elements and joins the result with
121
+ # slashes. This is used by <tt>url_for</tt> in Action Pack.
122
+ def to_param
123
+ collect { |e| e.to_param }.join '/'
124
+ end
125
125
  end
126
126
  </ruby>
127
127
 
@@ -30,7 +30,7 @@ Now, suppose we wanted to add a new order for an existing customer. We'd need to
30
30
  Or consider deleting a customer, and ensuring that all of its orders get deleted as well:
31
31
 
32
32
  <ruby>
33
- @orders = Order.find_all_by_customer_id(@customer.id)
33
+ @orders = Order.where(:customer_id => @customer.id)
34
34
  @orders.each do |order|
35
35
  order.destroy
36
36
  end
@@ -65,7 +65,7 @@ To learn more about the different types of associations, read the next section o
65
65
 
66
66
  h3. The Types of Associations
67
67
 
68
- 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 association:
68
+ 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:
69
69
 
70
70
  * +belongs_to+
71
71
  * +has_one+
@@ -165,6 +165,12 @@ class Paragraph < ActiveRecord::Base
165
165
  end
166
166
  </ruby>
167
167
 
168
+ With +:through => :sections+ specified, Rails will now understand:
169
+
170
+ <ruby>
171
+ @document.paragraphs
172
+ </ruby>
173
+
168
174
  h4. The +has_one :through+ Association
169
175
 
170
176
  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:
@@ -223,7 +229,7 @@ The corresponding migration might look like this:
223
229
 
224
230
  <ruby>
225
231
  class CreateSuppliers < ActiveRecord::Migration
226
- def self.up
232
+ def change
227
233
  create_table :suppliers do |t|
228
234
  t.string :name
229
235
  t.timestamps
@@ -235,11 +241,6 @@ class CreateSuppliers < ActiveRecord::Migration
235
241
  t.timestamps
236
242
  end
237
243
  end
238
-
239
- def self.down
240
- drop_table :accounts
241
- drop_table :suppliers
242
- end
243
244
  end
244
245
  </ruby>
245
246
 
@@ -308,7 +309,7 @@ If you have an instance of the +Picture+ model, you can get to its parent via +@
308
309
 
309
310
  <ruby>
310
311
  class CreatePictures < ActiveRecord::Migration
311
- def self.up
312
+ def change
312
313
  create_table :pictures do |t|
313
314
  t.string :name
314
315
  t.integer :imageable_id
@@ -316,10 +317,6 @@ class CreatePictures < ActiveRecord::Migration
316
317
  t.timestamps
317
318
  end
318
319
  end
319
-
320
- def self.down
321
- drop_table :pictures
322
- end
323
320
  end
324
321
  </ruby>
325
322
 
@@ -327,17 +324,13 @@ This migration can be simplified by using the +t.references+ form:
327
324
 
328
325
  <ruby>
329
326
  class CreatePictures < ActiveRecord::Migration
330
- def self.up
327
+ def change
331
328
  create_table :pictures do |t|
332
329
  t.string :name
333
330
  t.references :imageable, :polymorphic => true
334
331
  t.timestamps
335
332
  end
336
333
  end
337
-
338
- def self.down
339
- drop_table :pictures
340
- end
341
334
  end
342
335
  </ruby>
343
336
 
@@ -407,17 +400,13 @@ This declaration needs to be backed up by the proper foreign key declaration on
407
400
 
408
401
  <ruby>
409
402
  class CreateOrders < ActiveRecord::Migration
410
- def self.up
403
+ def change
411
404
  create_table :orders do |t|
412
405
  t.datetime :order_date
413
406
  t.string :order_number
414
407
  t.integer :customer_id
415
408
  end
416
409
  end
417
-
418
- def self.down
419
- drop_table :orders
420
- end
421
410
  end
422
411
  </ruby>
423
412
 
@@ -445,16 +434,12 @@ These need to be backed up by a migration to create the +assemblies_parts+ table
445
434
 
446
435
  <ruby>
447
436
  class CreateAssemblyPartJoinTable < ActiveRecord::Migration
448
- def self.up
437
+ def change
449
438
  create_table :assemblies_parts, :id => false do |t|
450
439
  t.integer :assembly_id
451
440
  t.integer :part_id
452
441
  end
453
442
  end
454
-
455
- def self.down
456
- drop_table :assemblies_parts
457
- end
458
443
  end
459
444
  </ruby>
460
445
 
@@ -550,7 +535,9 @@ build_customer
550
535
  create_customer
551
536
  </ruby>
552
537
 
553
- h6. <tt>_association_(force_reload = false)</tt>
538
+ 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.
539
+
540
+ h6(#belongs_to-association). <tt><em>association</em>(force_reload = false)</tt>
554
541
 
555
542
  The <tt><em>association</em></tt> method returns the associated object, if any. If no associated object is found, it returns +nil+.
556
543
 
@@ -560,7 +547,7 @@ The <tt><em>association</em></tt> method returns the associated object, if any.
560
547
 
561
548
  If the associated object has already been retrieved from the database for this object, the cached version will be returned. To override this behavior (and force a database read), pass +true+ as the +force_reload+ argument.
562
549
 
563
- h6. <tt>_association_=(associate)</tt>
550
+ h6(#belongs_to-association_equal). <tt>_association_=(associate)</tt>
564
551
 
565
552
  The <tt><em>association</em>=</tt> method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from the associate object and setting this object's foreign key to the same value.
566
553
 
@@ -637,7 +624,7 @@ class Order < ActiveRecord::Base
637
624
  end
638
625
  </ruby>
639
626
 
640
- h6. +:counter_cache+
627
+ h6(#belongs_to-counter_cache). +:counter_cache+
641
628
 
642
629
  The +:counter_cache+ option can be used to make finding the number of belonging objects more efficient. Consider these models:
643
630
 
@@ -733,7 +720,7 @@ end
733
720
 
734
721
  NOTE: There's no need to use +:include+ for immediate associations - that is, if you have +Order belongs_to :customer+, then the customer is eager-loaded automatically when it's needed.
735
722
 
736
- h6. +:polymorphic+
723
+ h6(#belongs_to-polymorphic). +:polymorphic+
737
724
 
738
725
  Passing +true+ to the +:polymorphic+ option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail <a href="#polymorphic-associations">earlier in this guide</a>.
739
726
 
@@ -747,7 +734,7 @@ The +:select+ option lets you override the SQL +SELECT+ clause that is used to r
747
734
 
748
735
  TIP: If you set the +:select+ option on a +belongs_to+ association, you should also set the +foreign_key+ option to guarantee the correct results.
749
736
 
750
- h6. +:touch+
737
+ h6(#belongs_to-touch). +:touch+
751
738
 
752
739
  If you set the +:touch+ option to +:true+, then the +updated_at+ or +updated_on+ timestamp on the associated object will be set to the current time whenever this object is saved or destroyed:
753
740
 
@@ -817,7 +804,9 @@ build_account
817
804
  create_account
818
805
  </ruby>
819
806
 
820
- h6. <tt><em>association</em>(force_reload = false)</tt>
807
+ 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.
808
+
809
+ h6(#has_one-association). <tt><em>association</em>(force_reload = false)</tt>
821
810
 
822
811
  The <tt><em>association</em></tt> method returns the associated object, if any. If no associated object is found, it returns +nil+.
823
812
 
@@ -827,7 +816,7 @@ The <tt><em>association</em></tt> method returns the associated object, if any.
827
816
 
828
817
  If the associated object has already been retrieved from the database for this object, the cached version will be returned. To override this behavior (and force a database read), pass +true+ as the +force_reload+ argument.
829
818
 
830
- h6. <tt><em>association</em>=(associate)</tt>
819
+ h6(#has_one-association_equal). <tt><em>association</em>=(associate)</tt>
831
820
 
832
821
  The <tt><em>association</em>=</tt> method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from this object and setting the associate object's foreign key to the same value.
833
822
 
@@ -1029,6 +1018,7 @@ When you declare a +has_many+ association, the declaring class automatically gai
1029
1018
  * <tt><em>collection</em>.empty?</tt>
1030
1019
  * <tt><em>collection</em>.size</tt>
1031
1020
  * <tt><em>collection</em>.find(...)</tt>
1021
+ * <tt><em>collection</em>.where(...)</tt>
1032
1022
  * <tt><em>collection</em>.exists?(...)</tt>
1033
1023
  * <tt><em>collection</em>.build(attributes = {}, ...)</tt>
1034
1024
  * <tt><em>collection</em>.create(attributes = {})</tt>
@@ -1054,6 +1044,7 @@ orders.clear
1054
1044
  orders.empty?
1055
1045
  orders.size
1056
1046
  orders.find(...)
1047
+ orders.where(...)
1057
1048
  orders.exists?(...)
1058
1049
  orders.build(attributes = {}, ...)
1059
1050
  orders.create(attributes = {})
@@ -1083,10 +1074,10 @@ The <tt><em>collection</em>.delete</tt> method removes one or more objects from
1083
1074
  @customer.orders.delete(@order1)
1084
1075
  </ruby>
1085
1076
 
1086
- WARNING: Objects will be in addition destroyed if they're associated with +:dependent => :destroy+, and deleted if they're associated with +:dependent => :delete_all+.
1077
+ WARNING: Additionally, objects will be destroyed if they're associated with +:dependent => :destroy+, and deleted if they're associated with +:dependent => :delete_all+.
1087
1078
 
1088
1079
 
1089
- h6(#has_many-collection_equal). <tt><em>collection</em>=objects</tt>
1080
+ h6(#has_many-collection-equal). <tt><em>collection</em>=objects</tt>
1090
1081
 
1091
1082
  The <tt><em>collection</em>=</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.
1092
1083
 
@@ -1102,11 +1093,11 @@ h6(#has_many-collection_singular_ids_ids). <tt><em>collection_singular</em>_ids=
1102
1093
 
1103
1094
  The <tt><em>collection_singular</em>_ids=</tt> method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.
1104
1095
 
1105
- h6(#has_many-collection_clear). <tt><em>collection</em>.clear</tt>
1096
+ h6(#has_many-collection-clear). <tt><em>collection</em>.clear</tt>
1106
1097
 
1107
1098
  The <tt><em>collection</em>.clear</tt> 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+.
1108
1099
 
1109
- h6. <tt><em>collection</em>.empty?</tt>
1100
+ h6(#has_many-collection-empty). <tt><em>collection</em>.empty?</tt>
1110
1101
 
1111
1102
  The <tt><em>collection</em>.empty?</tt> method returns +true+ if the collection does not contain any associated objects.
1112
1103
 
@@ -1116,7 +1107,7 @@ The <tt><em>collection</em>.empty?</tt> method returns +true+ if the collection
1116
1107
  <% end %>
1117
1108
  </ruby>
1118
1109
 
1119
- h6. <tt><em>collection</em>.size</tt>
1110
+ h6(#has_many-collection-size). <tt><em>collection</em>.size</tt>
1120
1111
 
1121
1112
  The <tt><em>collection</em>.size</tt> method returns the number of objects in the collection.
1122
1113
 
@@ -1124,19 +1115,30 @@ The <tt><em>collection</em>.size</tt> method returns the number of objects in th
1124
1115
  @order_count = @customer.orders.size
1125
1116
  </ruby>
1126
1117
 
1127
- h6. <tt><em>collection</em>.find(...)</tt>
1118
+ h6(#has_many-collection-find). <tt><em>collection</em>.find(...)</tt>
1128
1119
 
1129
1120
  The <tt><em>collection</em>.find</tt> method finds objects within the collection. It uses the same syntax and options as +ActiveRecord::Base.find+.
1130
1121
 
1131
1122
  <ruby>
1132
- @open_orders = @customer.orders.find(:all, :conditions => "open = 1")
1123
+ @open_orders = @customer.orders.all(:conditions => "open = 1")
1124
+ </ruby>
1125
+
1126
+ NOTE: Starting Rails 3, supplying options to +ActiveRecord::Base.find+ method is discouraged. Use <tt><em>collection</em>.where</tt> instead when you need to pass conditions.
1127
+
1128
+ h6(#has_many-collection-where). <tt><em>collection</em>.where(...)</tt>
1129
+
1130
+ The <tt><em>collection</em>.where</tt> method finds objects within the collection based on the conditions supplied but the objects are loaded lazily meaning that the database is queried only when the object(s) are accessed.
1131
+
1132
+ <ruby>
1133
+ @open_orders = @customer.orders.where(:open => true) # No query yet
1134
+ @open_order = @open_orders.first # Now the database will be queried
1133
1135
  </ruby>
1134
1136
 
1135
- h6. <tt><em>collection</em>.exists?(...)</tt>
1137
+ h6(#has_many-collection-exists). <tt><em>collection</em>.exists?(...)</tt>
1136
1138
 
1137
1139
  The <tt><em>collection</em>.exists?</tt> method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as +ActiveRecord::Base.exists?+.
1138
1140
 
1139
- h6(#has_many_collection_build). <tt><em>collection</em>.build(attributes = {}, ...)</tt>
1141
+ h6(#has_many-collection-build). <tt><em>collection</em>.build(attributes = {}, ...)</tt>
1140
1142
 
1141
1143
  The <tt><em>collection</em>.build</tt> method returns one or more new objects of the associated type. These objects will be instantiated from the passed attributes, and the link through their foreign key will be created, but the associated objects will _not_ yet be saved.
1142
1144
 
@@ -1145,7 +1147,7 @@ The <tt><em>collection</em>.build</tt> method returns one or more new objects of
1145
1147
  :order_number => "A12345")
1146
1148
  </ruby>
1147
1149
 
1148
- h6. <tt><em>collection</em>.create(attributes = {})</tt>
1150
+ h6(#has_many-collection-create). <tt><em>collection</em>.create(attributes = {})</tt>
1149
1151
 
1150
1152
  The <tt><em>collection</em>.create</tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, the link through its foreign key will be created, and the associated object _will_ be saved (assuming that it passes any validations).
1151
1153
 
@@ -1193,7 +1195,7 @@ h6(#has_many-as). +:as+
1193
1195
 
1194
1196
  Setting the +:as+ option indicates that this is a polymorphic association, as discussed <a href="#polymorphic-associations">earlier in this guide</a>.
1195
1197
 
1196
- h6. +:autosave+
1198
+ h6(#has_many-autosave). +:autosave+
1197
1199
 
1198
1200
  If you set the +:autosave+ option to +true+, Rails will save any loaded members and destroy members that are marked for destruction whenever you save the parent object.
1199
1201
 
@@ -1439,11 +1441,12 @@ When you declare a +has_and_belongs_to_many+ association, the declaring class au
1439
1441
  * <tt><em>collection</em>.empty?</tt>
1440
1442
  * <tt><em>collection</em>.size</tt>
1441
1443
  * <tt><em>collection</em>.find(...)</tt>
1444
+ * <tt><em>collection</em>.where(...)</tt>
1442
1445
  * <tt><em>collection</em>.exists?(...)</tt>
1443
1446
  * <tt><em>collection</em>.build(attributes = {})</tt>
1444
1447
  * <tt><em>collection</em>.create(attributes = {})</tt>
1445
1448
 
1446
- In all of these methods, <tt><em>collection</em></tt> is replaced with the symbol passed as the first argument to +has_and_belongs_to_many+, and <tt><em>collection_singular</em></tt> is replaced with the singularized version of that symbol.. For example, given the declaration:
1449
+ In all of these methods, <tt><em>collection</em></tt> is replaced with the symbol passed as the first argument to +has_and_belongs_to_many+, and <tt><em>collection_singular</em></tt> is replaced with the singularized version of that symbol. For example, given the declaration:
1447
1450
 
1448
1451
  <ruby>
1449
1452
  class Part < ActiveRecord::Base
@@ -1464,6 +1467,7 @@ assemblies.clear
1464
1467
  assemblies.empty?
1465
1468
  assemblies.size
1466
1469
  assemblies.find(...)
1470
+ assemblies.where(...)
1467
1471
  assemblies.exists?(...)
1468
1472
  assemblies.build(attributes = {}, ...)
1469
1473
  assemblies.create(attributes = {})
@@ -1476,7 +1480,7 @@ If the join table for a +has_and_belongs_to_many+ association has additional col
1476
1480
  WARNING: The use of extra attributes on the join table in a +has_and_belongs_to_many+ association is deprecated. If you require this sort of complex behavior on the table that joins two models in a many-to-many relationship, you should use a +has_many :through+ association instead of +has_and_belongs_to_many+.
1477
1481
 
1478
1482
 
1479
- h6. <tt><em>collection</em>(force_reload = false)</tt>
1483
+ h6(#has_and_belongs_to_many-collection). <tt><em>collection</em>(force_reload = false)</tt>
1480
1484
 
1481
1485
  The <tt><em>collection</em></tt> method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array.
1482
1486
 
@@ -1484,7 +1488,7 @@ The <tt><em>collection</em></tt> method returns an array of all of the associate
1484
1488
  @assemblies = @part.assemblies
1485
1489
  </ruby>
1486
1490
 
1487
- h6. <tt><em>collection</em><<(object, ...)</tt>
1491
+ h6(#has_and_belongs_to_many-collection-lt_lt). <tt><em>collection</em><<(object, ...)</tt>
1488
1492
 
1489
1493
  The <tt><em>collection</em><<</tt> method adds one or more objects to the collection by creating records in the join table.
1490
1494
 
@@ -1494,7 +1498,7 @@ The <tt><em>collection</em><<</tt> method adds one or more objects to the collec
1494
1498
 
1495
1499
  NOTE: This method is aliased as <tt><em>collection</em>.concat</tt> and <tt><em>collection</em>.push</tt>.
1496
1500
 
1497
- h6. <tt><em>collection</em>.delete(object, ...)</tt>
1501
+ h6(#has_and_belongs_to_many-collection-delete). <tt><em>collection</em>.delete(object, ...)</tt>
1498
1502
 
1499
1503
  The <tt><em>collection</em>.delete</tt> method removes one or more objects from the collection by deleting records in the join table. This does not destroy the objects.
1500
1504
 
@@ -1502,11 +1506,11 @@ The <tt><em>collection</em>.delete</tt> method removes one or more objects from
1502
1506
  @part.assemblies.delete(@assembly1)
1503
1507
  </ruby>
1504
1508
 
1505
- h6. <tt><em>collection</em>=objects</tt>
1509
+ h6(#has_and_belongs_to_many-collection-equal). <tt><em>collection</em>=objects</tt>
1506
1510
 
1507
1511
  The <tt><em>collection</em>=</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.
1508
1512
 
1509
- h6. <tt><em>collection_singular</em>_ids</tt>
1513
+ h6(#has_and_belongs_to_many-collection_singular). <tt><em>collection_singular</em>_ids</tt>
1510
1514
 
1511
1515
  The <tt><em>collection_singular</em>_ids</tt> method returns an array of the ids of the objects in the collection.
1512
1516
 
@@ -1514,11 +1518,11 @@ The <tt><em>collection_singular</em>_ids</tt> method returns an array of the ids
1514
1518
  @assembly_ids = @part.assembly_ids
1515
1519
  </ruby>
1516
1520
 
1517
- h6. <tt><em>collection_singular</em>_ids=ids</tt>
1521
+ h6(#has_and_belongs_to_many-collection_singular_ids_ids). <tt><em>collection_singular</em>_ids=ids</tt>
1518
1522
 
1519
1523
  The <tt><em>collection_singular</em>_ids=</tt> method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.
1520
1524
 
1521
- h6. <tt><em>collection</em>.clear</tt>
1525
+ h6(#has_and_belongs_to_many-collection-clear). <tt><em>collection</em>.clear</tt>
1522
1526
 
1523
1527
  The <tt><em>collection</em>.clear</tt> method removes every object from the collection by deleting the rows from the joining table. This does not destroy the associated objects.
1524
1528
 
@@ -1545,10 +1549,20 @@ h6(#has_and_belongs_to_many-collection-find). <tt><em>collection</em>.find(...)<
1545
1549
  The <tt><em>collection</em>.find</tt> method finds objects within the collection. It uses the same syntax and options as +ActiveRecord::Base.find+. It also adds the additional condition that the object must be in the collection.
1546
1550
 
1547
1551
  <ruby>
1548
- @new_assemblies = @part.assemblies.find(:all,
1552
+ @new_assemblies = @part.assemblies.all(
1549
1553
  :conditions => ["created_at > ?", 2.days.ago])
1550
1554
  </ruby>
1551
1555
 
1556
+ NOTE: Starting Rails 3, supplying options to +ActiveRecord::Base.find+ method is discouraged. Use <tt><em>collection</em>.where</tt> instead when you need to pass conditions.
1557
+
1558
+ h6(#has_and_belongs_to_many-collection-where). <tt><em>collection</em>.where(...)</tt>
1559
+
1560
+ The <tt><em>collection</em>.where</tt> method finds objects within the collection based on the conditions supplied but the objects are loaded lazily meaning that the database is queried only when the object(s) are accessed. It also adds the additional condition that the object must be in the collection.
1561
+
1562
+ <ruby>
1563
+ @new_assemblies = @part.assemblies.where("created_at > ?", 2.days.ago)
1564
+ </ruby>
1565
+
1552
1566
  h6(#has_and_belongs_to_many-collection-exists). <tt><em>collection</em>.exists?(...)</tt>
1553
1567
 
1554
1568
  The <tt><em>collection</em>.exists?</tt> method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as +ActiveRecord::Base.exists?+.
@@ -1605,7 +1619,7 @@ The +has_and_belongs_to_many+ association supports these options:
1605
1619
  * +:uniq+
1606
1620
  * +:validate+
1607
1621
 
1608
- h6. +:association_foreign_key+
1622
+ h6(#has_and_belongs_to_many-association_foreign_key). +:association_foreign_key+
1609
1623
 
1610
1624
  By convention, Rails guesses that the column in the join table used to hold the foreign key pointing to the other model is the name of that model with the suffix +_id+ added. The +:association_foreign_key+ option lets you set the name of the foreign key directly:
1611
1625
 
@@ -1661,7 +1675,7 @@ Normally Rails automatically generates the proper SQL to count the association m
1661
1675
 
1662
1676
  NOTE: If you specify +:finder_sql+ but not +:counter_sql+, then the counter SQL will be generated by substituting +SELECT COUNT(*) FROM+ for the +SELECT ... FROM+ clause of your +:finder_sql+ statement.
1663
1677
 
1664
- h6. +:delete_sql+
1678
+ h6(#has_and_belongs_to_many-delete_sql). +:delete_sql+
1665
1679
 
1666
1680
  Normally Rails automatically generates the proper SQL to remove links between the associated classes. With the +:delete_sql+ option, you can specify a complete SQL statement to delete them yourself.
1667
1681
 
@@ -1699,11 +1713,11 @@ h6(#has_and_belongs_to_many-include). +:include+
1699
1713
 
1700
1714
  You can use the +:include+ option to specify second-order associations that should be eager-loaded when this association is used.
1701
1715
 
1702
- h6. +:insert_sql+
1716
+ h6(#has_and_belongs_to_many-insert_sql). +:insert_sql+
1703
1717
 
1704
1718
  Normally Rails automatically generates the proper SQL to create links between the associated classes. With the +:insert_sql+ option, you can specify a complete SQL statement to insert them yourself.
1705
1719
 
1706
- h6. +:join_table+
1720
+ h6(#has_and_belongs_to_many-join_table). +:join_table+
1707
1721
 
1708
1722
  If the default name of the join table, based on lexical ordering, is not what you want, you can use the +:join_table+ option to override the default.
1709
1723
 
@@ -1821,7 +1835,7 @@ If you have an extension that should be shared by many associations, you can use
1821
1835
  <ruby>
1822
1836
  module FindRecentExtension
1823
1837
  def find_recent
1824
- find(:all, :conditions => ["created_at > ?", 5.days.ago])
1838
+ where("created_at > ?", 5.days.ago)
1825
1839
  end
1826
1840
  end
1827
1841