rails 4.0.13 → 4.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +22 -17
- data/guides/CHANGELOG.md +68 -34
- data/guides/assets/images/edge_badge.png +0 -0
- data/guides/assets/images/feature_tile.gif +0 -0
- data/guides/assets/images/footer_tile.gif +0 -0
- data/guides/assets/images/fxn.png +0 -0
- data/guides/assets/images/getting_started/article_with_comments.png +0 -0
- data/guides/assets/images/getting_started/challenge.png +0 -0
- data/guides/assets/images/getting_started/confirm_dialog.png +0 -0
- data/guides/assets/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
- data/guides/assets/images/getting_started/form_with_errors.png +0 -0
- data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
- data/guides/assets/images/getting_started/new_article.png +0 -0
- data/guides/assets/images/getting_started/rails_welcome.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
- data/guides/assets/images/getting_started/show_action_for_articles.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_articles_new.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_create_for_articles.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_new_for_articles.png +0 -0
- data/guides/assets/images/header_tile.gif +0 -0
- data/guides/assets/images/icons/README +1 -1
- data/guides/assets/images/icons/callouts/11.png +0 -0
- data/guides/assets/images/icons/callouts/12.png +0 -0
- data/guides/assets/images/icons/callouts/13.png +0 -0
- data/guides/assets/images/icons/callouts/15.png +0 -0
- data/guides/assets/images/icons/caution.png +0 -0
- data/guides/assets/images/icons/example.png +0 -0
- data/guides/assets/images/radar.png +0 -0
- data/guides/assets/images/rails4_features.png +0 -0
- data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
- data/guides/assets/images/vijaydev.jpg +0 -0
- data/guides/assets/javascripts/guides.js +30 -34
- data/guides/assets/stylesheets/main.css +2 -1
- data/guides/assets/stylesheets/print.css +1 -1
- data/guides/bug_report_templates/action_controller_gem.rb +9 -4
- data/guides/bug_report_templates/action_controller_master.rb +4 -2
- data/guides/bug_report_templates/active_record_gem.rb +5 -2
- data/guides/bug_report_templates/active_record_master.rb +2 -1
- data/guides/bug_report_templates/generic_gem.rb +15 -0
- data/guides/bug_report_templates/generic_master.rb +26 -0
- data/guides/code/getting_started/Gemfile +21 -24
- data/guides/code/getting_started/Gemfile.lock +78 -73
- data/guides/code/getting_started/Rakefile +1 -1
- data/guides/code/getting_started/app/assets/javascripts/application.js +1 -2
- data/guides/code/getting_started/app/views/layouts/application.html.erb +2 -2
- data/guides/code/getting_started/config/environment.rb +1 -1
- data/guides/code/getting_started/config/environments/development.rb +2 -2
- data/guides/code/getting_started/config/environments/production.rb +3 -3
- data/guides/code/getting_started/config/environments/test.rb +2 -2
- data/guides/code/getting_started/config/initializers/secret_token.rb +1 -1
- data/guides/code/getting_started/config/initializers/session_store.rb +1 -1
- data/guides/code/getting_started/config/routes.rb +1 -1
- data/guides/code/getting_started/config.ru +1 -1
- data/guides/code/getting_started/public/404.html +2 -0
- data/guides/code/getting_started/public/422.html +2 -0
- data/guides/code/getting_started/public/500.html +2 -0
- data/guides/code/getting_started/test/test_helper.rb +0 -3
- data/guides/rails_guides/helpers.rb +3 -1
- data/guides/source/2_2_release_notes.md +2 -2
- data/guides/source/2_3_release_notes.md +8 -8
- data/guides/source/3_0_release_notes.md +2 -3
- data/guides/source/3_1_release_notes.md +2 -2
- data/guides/source/3_2_release_notes.md +12 -12
- data/guides/source/4_0_release_notes.md +79 -46
- data/guides/source/4_1_release_notes.md +731 -0
- data/guides/source/_welcome.html.erb +5 -2
- data/guides/source/action_controller_overview.md +189 -40
- data/guides/source/action_mailer_basics.md +27 -27
- data/guides/source/action_view_overview.md +131 -20
- data/guides/source/active_model_basics.md +6 -6
- data/guides/source/active_record_basics.md +15 -15
- data/guides/source/active_record_callbacks.md +18 -16
- data/guides/source/active_record_querying.md +93 -51
- data/guides/source/active_record_validations.md +26 -24
- data/guides/source/active_support_core_extensions.md +72 -118
- data/guides/source/active_support_instrumentation.md +13 -4
- data/guides/source/api_documentation_guidelines.md +104 -6
- data/guides/source/asset_pipeline.md +573 -244
- data/guides/source/association_basics.md +94 -22
- data/guides/source/caching_with_rails.md +15 -6
- data/guides/source/command_line.md +55 -46
- data/guides/source/configuring.md +248 -52
- data/guides/source/contributing_to_ruby_on_rails.md +18 -17
- data/guides/source/credits.html.erb +2 -2
- data/guides/source/debugging_rails_applications.md +39 -8
- data/guides/source/development_dependencies_install.md +91 -8
- data/guides/source/documents.yaml +4 -0
- data/guides/source/engines.md +678 -232
- data/guides/source/form_helpers.md +53 -35
- data/guides/source/generators.md +19 -15
- data/guides/source/getting_started.md +758 -497
- data/guides/source/i18n.md +64 -28
- data/guides/source/index.html.erb +1 -1
- data/guides/source/initialization.md +155 -58
- data/guides/source/kindle/toc.html.erb +1 -1
- data/guides/source/layout.html.erb +2 -2
- data/guides/source/layouts_and_rendering.md +59 -26
- data/guides/source/maintenance_policy.md +3 -3
- data/guides/source/migrations.md +101 -62
- data/guides/source/nested_model_forms.md +3 -3
- data/guides/source/plugins.md +34 -31
- data/guides/source/rails_application_templates.md +27 -8
- data/guides/source/rails_on_rack.md +41 -58
- data/guides/source/routing.md +115 -104
- data/guides/source/ruby_on_rails_guides_guidelines.md +2 -2
- data/guides/source/security.md +81 -36
- data/guides/source/testing.md +56 -79
- data/guides/source/upgrading_ruby_on_rails.md +531 -21
- data/guides/source/working_with_javascript_in_rails.md +19 -11
- metadata +51 -23
- data/guides/assets/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
- data/guides/assets/images/getting_started/new_post.png +0 -0
- data/guides/assets/images/getting_started/post_with_comments.png +0 -0
- data/guides/assets/images/getting_started/show_action_for_posts.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
- data/guides/assets/images/getting_started/undefined_method_post_path.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_create_for_posts.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_new_for_posts.png +0 -0
- data/guides/assets/images/jaimeiniesta.jpg +0 -0
- 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
|
|
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 Key
|
|
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`
|
|
@@ -340,7 +340,7 @@ class CreateAssembliesAndParts < ActiveRecord::Migration
|
|
|
340
340
|
t.timestamps
|
|
341
341
|
end
|
|
342
342
|
|
|
343
|
-
create_table :assemblies_parts do |t|
|
|
343
|
+
create_table :assemblies_parts, id: false do |t|
|
|
344
344
|
t.belongs_to :assembly
|
|
345
345
|
t.belongs_to :part
|
|
346
346
|
end
|
|
@@ -490,6 +490,19 @@ end
|
|
|
490
490
|
|
|
491
491
|
With this setup, you can retrieve `@employee.subordinates` and `@employee.manager`.
|
|
492
492
|
|
|
493
|
+
In your migrations/schema, you will add a references column to the model itself.
|
|
494
|
+
|
|
495
|
+
```ruby
|
|
496
|
+
class CreateEmployees < ActiveRecord::Migration
|
|
497
|
+
def change
|
|
498
|
+
create_table :employees do |t|
|
|
499
|
+
t.references :manager
|
|
500
|
+
t.timestamps
|
|
501
|
+
end
|
|
502
|
+
end
|
|
503
|
+
end
|
|
504
|
+
```
|
|
505
|
+
|
|
493
506
|
Tips, Tricks, and Warnings
|
|
494
507
|
--------------------------
|
|
495
508
|
|
|
@@ -696,6 +709,17 @@ There are a few limitations to `inverse_of` support:
|
|
|
696
709
|
* They do not work with `:as` associations.
|
|
697
710
|
* For `belongs_to` associations, `has_many` inverse associations are ignored.
|
|
698
711
|
|
|
712
|
+
Every association will attempt to automatically find the inverse association
|
|
713
|
+
and set the `:inverse_of` option heuristically (based on the association name).
|
|
714
|
+
Most associations with standard names will be supported. However, associations
|
|
715
|
+
that contain the following options will not have their inverses set
|
|
716
|
+
automatically:
|
|
717
|
+
|
|
718
|
+
* :conditions
|
|
719
|
+
* :through
|
|
720
|
+
* :polymorphic
|
|
721
|
+
* :foreign_key
|
|
722
|
+
|
|
699
723
|
Detailed Association Reference
|
|
700
724
|
------------------------------
|
|
701
725
|
|
|
@@ -707,12 +731,13 @@ The `belongs_to` association creates a one-to-one match with another model. In d
|
|
|
707
731
|
|
|
708
732
|
#### Methods Added by `belongs_to`
|
|
709
733
|
|
|
710
|
-
When you declare a `belongs_to` association, the declaring class automatically gains
|
|
734
|
+
When you declare a `belongs_to` association, the declaring class automatically gains five methods related to the association:
|
|
711
735
|
|
|
712
736
|
* `association(force_reload = false)`
|
|
713
737
|
* `association=(associate)`
|
|
714
738
|
* `build_association(attributes = {})`
|
|
715
739
|
* `create_association(attributes = {})`
|
|
740
|
+
* `create_association!(attributes = {})`
|
|
716
741
|
|
|
717
742
|
In all of these methods, `association` is replaced with the symbol passed as the first argument to `belongs_to`. For example, given the declaration:
|
|
718
743
|
|
|
@@ -729,6 +754,7 @@ customer
|
|
|
729
754
|
customer=
|
|
730
755
|
build_customer
|
|
731
756
|
create_customer
|
|
757
|
+
create_customer!
|
|
732
758
|
```
|
|
733
759
|
|
|
734
760
|
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.
|
|
@@ -769,6 +795,10 @@ The `create_association` method returns a new object of the associated type. Thi
|
|
|
769
795
|
customer_name: "John Doe")
|
|
770
796
|
```
|
|
771
797
|
|
|
798
|
+
##### `create_association!(attributes = {})`
|
|
799
|
+
|
|
800
|
+
Does the same as `create_association` above, but raises `ActiveRecord::RecordInvalid` if the record is invalid.
|
|
801
|
+
|
|
772
802
|
|
|
773
803
|
#### Options for `belongs_to`
|
|
774
804
|
|
|
@@ -847,8 +877,12 @@ end
|
|
|
847
877
|
Counter cache columns are added to the containing model's list of read-only attributes through `attr_readonly`.
|
|
848
878
|
|
|
849
879
|
##### `:dependent`
|
|
880
|
+
If you set the `:dependent` option to:
|
|
850
881
|
|
|
851
|
-
|
|
882
|
+
* `:destroy`, when the object is destroyed, `destroy` will be called on its
|
|
883
|
+
associated objects.
|
|
884
|
+
* `:delete`, when the object is destroyed, all its associated objects will be
|
|
885
|
+
deleted directly from the database without calling their `destroy` method.
|
|
852
886
|
|
|
853
887
|
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.
|
|
854
888
|
|
|
@@ -939,7 +973,7 @@ end
|
|
|
939
973
|
|
|
940
974
|
##### `includes`
|
|
941
975
|
|
|
942
|
-
You can use the `includes` method
|
|
976
|
+
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:
|
|
943
977
|
|
|
944
978
|
```ruby
|
|
945
979
|
class LineItem < ActiveRecord::Base
|
|
@@ -1005,12 +1039,13 @@ The `has_one` association creates a one-to-one match with another model. In data
|
|
|
1005
1039
|
|
|
1006
1040
|
#### Methods Added by `has_one`
|
|
1007
1041
|
|
|
1008
|
-
When you declare a `has_one` association, the declaring class automatically gains
|
|
1042
|
+
When you declare a `has_one` association, the declaring class automatically gains five methods related to the association:
|
|
1009
1043
|
|
|
1010
1044
|
* `association(force_reload = false)`
|
|
1011
1045
|
* `association=(associate)`
|
|
1012
1046
|
* `build_association(attributes = {})`
|
|
1013
1047
|
* `create_association(attributes = {})`
|
|
1048
|
+
* `create_association!(attributes = {})`
|
|
1014
1049
|
|
|
1015
1050
|
In all of these methods, `association` is replaced with the symbol passed as the first argument to `has_one`. For example, given the declaration:
|
|
1016
1051
|
|
|
@@ -1027,6 +1062,7 @@ account
|
|
|
1027
1062
|
account=
|
|
1028
1063
|
build_account
|
|
1029
1064
|
create_account
|
|
1065
|
+
create_account!
|
|
1030
1066
|
```
|
|
1031
1067
|
|
|
1032
1068
|
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.
|
|
@@ -1065,6 +1101,10 @@ The `create_association` method returns a new object of the associated type. Thi
|
|
|
1065
1101
|
@account = @supplier.create_account(terms: "Net 30")
|
|
1066
1102
|
```
|
|
1067
1103
|
|
|
1104
|
+
##### `create_association!(attributes = {})`
|
|
1105
|
+
|
|
1106
|
+
Does the same as `create_association` above, but raises `ActiveRecord::RecordInvalid` if the record is invalid.
|
|
1107
|
+
|
|
1068
1108
|
#### Options for `has_one`
|
|
1069
1109
|
|
|
1070
1110
|
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:
|
|
@@ -1117,6 +1157,12 @@ Controls what happens to the associated object when its owner is destroyed:
|
|
|
1117
1157
|
* `:restrict_with_exception` causes an exception to be raised if there is an associated record
|
|
1118
1158
|
* `:restrict_with_error` causes an error to be added to the owner if there is an associated object
|
|
1119
1159
|
|
|
1160
|
+
It's necessary not to set or leave `:nullify` option for those associations
|
|
1161
|
+
that have `NOT NULL` database constraints. If you don't set `dependent` to
|
|
1162
|
+
destroy such associations you won't be able to change the associated object
|
|
1163
|
+
because initial associated object foreign key will be set to unallowed `NULL`
|
|
1164
|
+
value.
|
|
1165
|
+
|
|
1120
1166
|
##### `:foreign_key`
|
|
1121
1167
|
|
|
1122
1168
|
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:
|
|
@@ -1260,7 +1306,7 @@ The `has_many` association creates a one-to-many relationship with another model
|
|
|
1260
1306
|
|
|
1261
1307
|
#### Methods Added by `has_many`
|
|
1262
1308
|
|
|
1263
|
-
When you declare a `has_many` association, the declaring class automatically gains
|
|
1309
|
+
When you declare a `has_many` association, the declaring class automatically gains 16 methods related to the association:
|
|
1264
1310
|
|
|
1265
1311
|
* `collection(force_reload = false)`
|
|
1266
1312
|
* `collection<<(object, ...)`
|
|
@@ -1277,6 +1323,7 @@ When you declare a `has_many` association, the declaring class automatically gai
|
|
|
1277
1323
|
* `collection.exists?(...)`
|
|
1278
1324
|
* `collection.build(attributes = {}, ...)`
|
|
1279
1325
|
* `collection.create(attributes = {})`
|
|
1326
|
+
* `collection.create!(attributes = {})`
|
|
1280
1327
|
|
|
1281
1328
|
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:
|
|
1282
1329
|
|
|
@@ -1304,6 +1351,7 @@ orders.where(...)
|
|
|
1304
1351
|
orders.exists?(...)
|
|
1305
1352
|
orders.build(attributes = {}, ...)
|
|
1306
1353
|
orders.create(attributes = {})
|
|
1354
|
+
orders.create!(attributes = {})
|
|
1307
1355
|
```
|
|
1308
1356
|
|
|
1309
1357
|
##### `collection(force_reload = false)`
|
|
@@ -1419,6 +1467,10 @@ The `collection.create` method returns a new object of the associated type. This
|
|
|
1419
1467
|
order_number: "A12345")
|
|
1420
1468
|
```
|
|
1421
1469
|
|
|
1470
|
+
##### `collection.create!(attributes = {})`
|
|
1471
|
+
|
|
1472
|
+
Does the same as `collection.create` above, but raises `ActiveRecord::RecordInvalid` if the record is invalid.
|
|
1473
|
+
|
|
1422
1474
|
#### Options for `has_many`
|
|
1423
1475
|
|
|
1424
1476
|
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:
|
|
@@ -1503,6 +1555,20 @@ end
|
|
|
1503
1555
|
|
|
1504
1556
|
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.
|
|
1505
1557
|
|
|
1558
|
+
Let's say that `users` table has `id` as the primary_key but it also has
|
|
1559
|
+
`guid` column. And the requirement is that `todos` table should hold
|
|
1560
|
+
`guid` column value and not `id` value. This can be achieved like this
|
|
1561
|
+
|
|
1562
|
+
```ruby
|
|
1563
|
+
class User < ActiveRecord::Base
|
|
1564
|
+
has_many :todos, primary_key: :guid
|
|
1565
|
+
end
|
|
1566
|
+
```
|
|
1567
|
+
|
|
1568
|
+
Now if we execute `@user.todos.create` then `@todo` record will have
|
|
1569
|
+
`user_id` value as the `guid` value of `@user`.
|
|
1570
|
+
|
|
1571
|
+
|
|
1506
1572
|
##### `:source`
|
|
1507
1573
|
|
|
1508
1574
|
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.
|
|
@@ -1670,7 +1736,7 @@ person.posts.inspect # => [#<Post id: 5, name: "a1">, #<Post id: 5, name: "a1">]
|
|
|
1670
1736
|
Reading.all.inspect # => [#<Reading id: 12, person_id: 5, post_id: 5>, #<Reading id: 13, person_id: 5, post_id: 5>]
|
|
1671
1737
|
```
|
|
1672
1738
|
|
|
1673
|
-
In the above case there are two readings and `person.posts` brings out both of
|
|
1739
|
+
In the above case there are two readings and `person.posts` brings out both of
|
|
1674
1740
|
them even though these records are pointing to the same post.
|
|
1675
1741
|
|
|
1676
1742
|
Now let's set `distinct`:
|
|
@@ -1689,24 +1755,24 @@ person.posts.inspect # => [#<Post id: 7, name: "a1">]
|
|
|
1689
1755
|
Reading.all.inspect # => [#<Reading id: 16, person_id: 7, post_id: 7>, #<Reading id: 17, person_id: 7, post_id: 7>]
|
|
1690
1756
|
```
|
|
1691
1757
|
|
|
1692
|
-
In the above case there are still two readings. However `person.posts` shows
|
|
1758
|
+
In the above case there are still two readings. However `person.posts` shows
|
|
1693
1759
|
only one post because the collection loads only unique records.
|
|
1694
1760
|
|
|
1695
|
-
If you want to make sure that, upon insertion, all of the records in the
|
|
1696
|
-
persisted association are distinct (so that you can be sure that when you
|
|
1697
|
-
inspect the association that you will never find duplicate records), you should
|
|
1698
|
-
add a unique index on the table itself. For example, if you have a table named
|
|
1699
|
-
|
|
1761
|
+
If you want to make sure that, upon insertion, all of the records in the
|
|
1762
|
+
persisted association are distinct (so that you can be sure that when you
|
|
1763
|
+
inspect the association that you will never find duplicate records), you should
|
|
1764
|
+
add a unique index on the table itself. For example, if you have a table named
|
|
1765
|
+
`person_posts` and you want to make sure all the posts are unique, you could
|
|
1700
1766
|
add the following in a migration:
|
|
1701
1767
|
|
|
1702
1768
|
```ruby
|
|
1703
|
-
add_index :person_posts, :post, :
|
|
1769
|
+
add_index :person_posts, :post, unique: true
|
|
1704
1770
|
```
|
|
1705
1771
|
|
|
1706
|
-
Note that checking for uniqueness using something like
|
|
1707
|
-
to race conditions. Do not attempt to use
|
|
1708
|
-
in an association. For instance, using the post example from above, the
|
|
1709
|
-
following code would be racy because multiple users could be attempting this
|
|
1772
|
+
Note that checking for uniqueness using something like `include?` is subject
|
|
1773
|
+
to race conditions. Do not attempt to use `include?` to enforce distinctness
|
|
1774
|
+
in an association. For instance, using the post example from above, the
|
|
1775
|
+
following code would be racy because multiple users could be attempting this
|
|
1710
1776
|
at the same time:
|
|
1711
1777
|
|
|
1712
1778
|
```ruby
|
|
@@ -1729,7 +1795,7 @@ The `has_and_belongs_to_many` association creates a many-to-many relationship wi
|
|
|
1729
1795
|
|
|
1730
1796
|
#### Methods Added by `has_and_belongs_to_many`
|
|
1731
1797
|
|
|
1732
|
-
When you declare a `has_and_belongs_to_many` association, the declaring class automatically gains
|
|
1798
|
+
When you declare a `has_and_belongs_to_many` association, the declaring class automatically gains 16 methods related to the association:
|
|
1733
1799
|
|
|
1734
1800
|
* `collection(force_reload = false)`
|
|
1735
1801
|
* `collection<<(object, ...)`
|
|
@@ -1746,6 +1812,7 @@ When you declare a `has_and_belongs_to_many` association, the declaring class au
|
|
|
1746
1812
|
* `collection.exists?(...)`
|
|
1747
1813
|
* `collection.build(attributes = {})`
|
|
1748
1814
|
* `collection.create(attributes = {})`
|
|
1815
|
+
* `collection.create!(attributes = {})`
|
|
1749
1816
|
|
|
1750
1817
|
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:
|
|
1751
1818
|
|
|
@@ -1773,6 +1840,7 @@ assemblies.where(...)
|
|
|
1773
1840
|
assemblies.exists?(...)
|
|
1774
1841
|
assemblies.build(attributes = {}, ...)
|
|
1775
1842
|
assemblies.create(attributes = {})
|
|
1843
|
+
assemblies.create!(attributes = {})
|
|
1776
1844
|
```
|
|
1777
1845
|
|
|
1778
1846
|
##### Additional Column Methods
|
|
@@ -1892,6 +1960,10 @@ The `collection.create` method returns a new object of the associated type. This
|
|
|
1892
1960
|
@assembly = @part.assemblies.create({assembly_name: "Transmission housing"})
|
|
1893
1961
|
```
|
|
1894
1962
|
|
|
1963
|
+
##### `collection.create!(attributes = {})`
|
|
1964
|
+
|
|
1965
|
+
Does the same as `collection.create`, but raises `ActiveRecord::RecordInvalid` if the record is invalid.
|
|
1966
|
+
|
|
1895
1967
|
#### Options for `has_and_belongs_to_many`
|
|
1896
1968
|
|
|
1897
1969
|
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:
|
|
@@ -1921,7 +1993,7 @@ TIP: The `:foreign_key` and `:association_foreign_key` options are useful when s
|
|
|
1921
1993
|
|
|
1922
1994
|
```ruby
|
|
1923
1995
|
class User < ActiveRecord::Base
|
|
1924
|
-
has_and_belongs_to_many :friends,
|
|
1996
|
+
has_and_belongs_to_many :friends,
|
|
1925
1997
|
class_name: "User",
|
|
1926
1998
|
foreign_key: "this_user_id",
|
|
1927
1999
|
association_foreign_key: "other_user_id"
|
|
@@ -104,6 +104,15 @@ This method generates a cache key that depends on all products and can be used i
|
|
|
104
104
|
All available products:
|
|
105
105
|
<% end %>
|
|
106
106
|
```
|
|
107
|
+
|
|
108
|
+
If you want to cache a fragment under certain condition you can use `cache_if` or `cache_unless`
|
|
109
|
+
|
|
110
|
+
```erb
|
|
111
|
+
<% cache_if (condition, cache_key_for_products) do %>
|
|
112
|
+
All available products:
|
|
113
|
+
<% end %>
|
|
114
|
+
```
|
|
115
|
+
|
|
107
116
|
You can also use an Active Record model as the cache key:
|
|
108
117
|
|
|
109
118
|
```erb
|
|
@@ -180,7 +189,7 @@ The main methods to call are `read`, `write`, `delete`, `exist?`, and `fetch`. T
|
|
|
180
189
|
|
|
181
190
|
There are some common options used by all cache implementations. These can be passed to the constructor or the various methods to interact with entries.
|
|
182
191
|
|
|
183
|
-
* `:namespace` - This option can be used to create a namespace within the cache store. It is especially useful if your application shares a cache with other applications.
|
|
192
|
+
* `:namespace` - This option can be used to create a namespace within the cache store. It is especially useful if your application shares a cache with other applications.
|
|
184
193
|
|
|
185
194
|
* `:compress` - This option can be used to indicate that compression should be used in the cache. This can be useful for transferring large cache entries over a slow network.
|
|
186
195
|
|
|
@@ -216,7 +225,7 @@ This is the default cache store implementation.
|
|
|
216
225
|
|
|
217
226
|
### ActiveSupport::Cache::MemCacheStore
|
|
218
227
|
|
|
219
|
-
This cache store uses Danga's `memcached` server to provide a centralized cache for your application. Rails uses the bundled `dalli` gem by default. This is currently the most popular cache store for production websites. It can be used to provide a single, shared cache cluster with very
|
|
228
|
+
This cache store uses Danga's `memcached` server to provide a centralized cache for your application. Rails uses the bundled `dalli` gem by default. This is currently the most popular cache store for production websites. It can be used to provide a single, shared cache cluster with very high performance and redundancy.
|
|
220
229
|
|
|
221
230
|
When initializing the cache, you need to specify the addresses for all memcached servers in your cluster. If none is specified, it will assume memcached is running on the local host on the default port, but this is not an ideal set up for larger sites.
|
|
222
231
|
|
|
@@ -236,7 +245,7 @@ config.cache_store = :ehcache_store
|
|
|
236
245
|
|
|
237
246
|
When initializing the cache, you may use the `:ehcache_config` option to specify the Ehcache config file to use (where the default is "ehcache.xml" in your Rails config directory), and the :cache_name option to provide a custom name for your cache (the default is rails_cache).
|
|
238
247
|
|
|
239
|
-
In addition to the standard `:expires_in` option, the `write` method on this cache can also accept the additional
|
|
248
|
+
In addition to the standard `:expires_in` option, the `write` method on this cache can also accept the additional `:unless_exist` option, which will cause the cache store to use Ehcache's `putIfAbsent` method instead of `put`, and therefore will not overwrite an existing entry. Additionally, the `write` method supports all of the properties exposed by the [Ehcache Element class](http://ehcache.org/apidocs/net/sf/ehcache/Element.html) , including:
|
|
240
249
|
|
|
241
250
|
| Property | Argument Type | Description |
|
|
242
251
|
| --------------------------- | ------------------- | ----------------------------------------------------------- |
|
|
@@ -292,7 +301,7 @@ Conditional GET support
|
|
|
292
301
|
|
|
293
302
|
Conditional GETs are a feature of the HTTP specification that provide a way for web servers to tell browsers that the response to a GET request hasn't changed since the last request and can be safely pulled from the browser cache.
|
|
294
303
|
|
|
295
|
-
They work by using the `HTTP_IF_NONE_MATCH` and `HTTP_IF_MODIFIED_SINCE` headers to pass back and forth both a unique content identifier and the timestamp of when the content was last changed. If the browser makes a request where the content identifier (etag) or last modified since timestamp matches the server
|
|
304
|
+
They work by using the `HTTP_IF_NONE_MATCH` and `HTTP_IF_MODIFIED_SINCE` headers to pass back and forth both a unique content identifier and the timestamp of when the content was last changed. If the browser makes a request where the content identifier (etag) or last modified since timestamp matches the server's version then the server only needs to send back an empty response with a not modified status.
|
|
296
305
|
|
|
297
306
|
It is the server's (i.e. our) responsibility to look for a last modified timestamp and the if-none-match header and determine whether or not to send back the full response. With conditional-get support in Rails this is a pretty easy task:
|
|
298
307
|
|
|
@@ -318,7 +327,7 @@ class ProductsController < ApplicationController
|
|
|
318
327
|
end
|
|
319
328
|
```
|
|
320
329
|
|
|
321
|
-
Instead of
|
|
330
|
+
Instead of an options hash, you can also simply pass in a model, Rails will use the `updated_at` and `cache_key` methods for setting `last_modified` and `etag`:
|
|
322
331
|
|
|
323
332
|
```ruby
|
|
324
333
|
class ProductsController < ApplicationController
|
|
@@ -329,7 +338,7 @@ class ProductsController < ApplicationController
|
|
|
329
338
|
end
|
|
330
339
|
```
|
|
331
340
|
|
|
332
|
-
If you don't have any special response processing and are using the default rendering mechanism (i.e. you're not using respond_to or calling render yourself) then you
|
|
341
|
+
If you don't have any special response processing and are using the default rendering mechanism (i.e. you're not using respond_to or calling render yourself) then you've got an easy helper in fresh_when:
|
|
333
342
|
|
|
334
343
|
```ruby
|
|
335
344
|
class ProductsController < ApplicationController
|