rails 4.0.13 → 4.1.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rails might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +20 -15
- data/guides/CHANGELOG.md +5 -74
- 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/challenge.png +0 -0
- 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/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/template_is_missing_posts_new.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/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 +2 -0
- data/guides/bug_report_templates/action_controller_master.rb +2 -0
- data/guides/bug_report_templates/active_record_gem.rb +1 -1
- data/guides/bug_report_templates/active_record_master.rb +2 -1
- data/guides/code/getting_started/Gemfile +1 -1
- data/guides/code/getting_started/app/assets/javascripts/application.js +1 -2
- data/guides/code/getting_started/config/environments/development.rb +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/rails_guides/helpers.rb +1 -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 +1 -2
- data/guides/source/3_1_release_notes.md +1 -1
- 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 +601 -0
- data/guides/source/_welcome.html.erb +1 -1
- data/guides/source/action_controller_overview.md +117 -31
- data/guides/source/action_mailer_basics.md +19 -19
- data/guides/source/action_view_overview.md +131 -12
- 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 +67 -39
- data/guides/source/active_record_validations.md +31 -31
- data/guides/source/active_support_core_extensions.md +63 -74
- data/guides/source/active_support_instrumentation.md +13 -4
- data/guides/source/api_documentation_guidelines.md +19 -5
- data/guides/source/asset_pipeline.md +544 -249
- data/guides/source/association_basics.md +81 -22
- data/guides/source/caching_with_rails.md +15 -6
- data/guides/source/command_line.md +28 -19
- data/guides/source/configuring.md +98 -50
- data/guides/source/contributing_to_ruby_on_rails.md +11 -11
- data/guides/source/credits.html.erb +2 -2
- data/guides/source/debugging_rails_applications.md +36 -5
- data/guides/source/development_dependencies_install.md +89 -8
- data/guides/source/documents.yaml +7 -1
- data/guides/source/engines.md +648 -224
- data/guides/source/form_helpers.md +56 -45
- data/guides/source/generators.md +7 -3
- data/guides/source/getting_started.md +379 -164
- data/guides/source/i18n.md +59 -23
- data/guides/source/index.html.erb +1 -1
- data/guides/source/initialization.md +153 -56
- data/guides/source/kindle/toc.html.erb +1 -1
- data/guides/source/layout.html.erb +3 -3
- data/guides/source/layouts_and_rendering.md +12 -11
- data/guides/source/maintenance_policy.md +4 -23
- data/guides/source/migrations.md +41 -37
- data/guides/source/nested_model_forms.md +3 -3
- data/guides/source/plugins.md +27 -23
- data/guides/source/rails_application_templates.md +25 -6
- data/guides/source/rails_on_rack.md +35 -51
- data/guides/source/routing.md +108 -99
- data/guides/source/ruby_on_rails_guides_guidelines.md +2 -2
- data/guides/source/security.md +33 -31
- data/guides/source/testing.md +37 -34
- data/guides/source/upgrading_ruby_on_rails.md +335 -16
- data/guides/source/working_with_javascript_in_rails.md +18 -10
- metadata +66 -39
- 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
|
@@ -696,6 +696,17 @@ There are a few limitations to `inverse_of` support:
|
|
696
696
|
* They do not work with `:as` associations.
|
697
697
|
* For `belongs_to` associations, `has_many` inverse associations are ignored.
|
698
698
|
|
699
|
+
Every association will attempt to automatically find the inverse association
|
700
|
+
and set the `:inverse_of` option heuristically (based on the association name).
|
701
|
+
Most associations with standard names will be supported. However, associations
|
702
|
+
that contain the following options will not have their inverses set
|
703
|
+
automatically:
|
704
|
+
|
705
|
+
* :conditions
|
706
|
+
* :through
|
707
|
+
* :polymorphic
|
708
|
+
* :foreign_key
|
709
|
+
|
699
710
|
Detailed Association Reference
|
700
711
|
------------------------------
|
701
712
|
|
@@ -707,12 +718,13 @@ The `belongs_to` association creates a one-to-one match with another model. In d
|
|
707
718
|
|
708
719
|
#### Methods Added by `belongs_to`
|
709
720
|
|
710
|
-
When you declare a `belongs_to` association, the declaring class automatically gains
|
721
|
+
When you declare a `belongs_to` association, the declaring class automatically gains five methods related to the association:
|
711
722
|
|
712
723
|
* `association(force_reload = false)`
|
713
724
|
* `association=(associate)`
|
714
725
|
* `build_association(attributes = {})`
|
715
726
|
* `create_association(attributes = {})`
|
727
|
+
* `create_association!(attributes = {})`
|
716
728
|
|
717
729
|
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
730
|
|
@@ -729,6 +741,7 @@ customer
|
|
729
741
|
customer=
|
730
742
|
build_customer
|
731
743
|
create_customer
|
744
|
+
create_customer!
|
732
745
|
```
|
733
746
|
|
734
747
|
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 +782,10 @@ The `create_association` method returns a new object of the associated type. Thi
|
|
769
782
|
customer_name: "John Doe")
|
770
783
|
```
|
771
784
|
|
785
|
+
##### `create_association!(attributes = {})`
|
786
|
+
|
787
|
+
Does the same as `create_association` above, but raises `ActiveRecord::RecordInvalid` if the record is invalid.
|
788
|
+
|
772
789
|
|
773
790
|
#### Options for `belongs_to`
|
774
791
|
|
@@ -847,8 +864,12 @@ end
|
|
847
864
|
Counter cache columns are added to the containing model's list of read-only attributes through `attr_readonly`.
|
848
865
|
|
849
866
|
##### `:dependent`
|
867
|
+
If you set the `:dependent` option to:
|
850
868
|
|
851
|
-
|
869
|
+
* `:destroy`, when the object is destroyed, `destroy` will be called on its
|
870
|
+
associated objects.
|
871
|
+
* `:delete`, when the object is destroyed, all its associated objects will be
|
872
|
+
deleted directly from the database without calling their `destroy` method.
|
852
873
|
|
853
874
|
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
875
|
|
@@ -939,7 +960,7 @@ end
|
|
939
960
|
|
940
961
|
##### `includes`
|
941
962
|
|
942
|
-
You can use the `includes` method
|
963
|
+
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
964
|
|
944
965
|
```ruby
|
945
966
|
class LineItem < ActiveRecord::Base
|
@@ -1005,12 +1026,13 @@ The `has_one` association creates a one-to-one match with another model. In data
|
|
1005
1026
|
|
1006
1027
|
#### Methods Added by `has_one`
|
1007
1028
|
|
1008
|
-
When you declare a `has_one` association, the declaring class automatically gains
|
1029
|
+
When you declare a `has_one` association, the declaring class automatically gains five methods related to the association:
|
1009
1030
|
|
1010
1031
|
* `association(force_reload = false)`
|
1011
1032
|
* `association=(associate)`
|
1012
1033
|
* `build_association(attributes = {})`
|
1013
1034
|
* `create_association(attributes = {})`
|
1035
|
+
* `create_association!(attributes = {})`
|
1014
1036
|
|
1015
1037
|
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
1038
|
|
@@ -1027,6 +1049,7 @@ account
|
|
1027
1049
|
account=
|
1028
1050
|
build_account
|
1029
1051
|
create_account
|
1052
|
+
create_account!
|
1030
1053
|
```
|
1031
1054
|
|
1032
1055
|
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 +1088,10 @@ The `create_association` method returns a new object of the associated type. Thi
|
|
1065
1088
|
@account = @supplier.create_account(terms: "Net 30")
|
1066
1089
|
```
|
1067
1090
|
|
1091
|
+
##### `create_association!(attributes = {})`
|
1092
|
+
|
1093
|
+
Does the same as `create_association` above, but raises `ActiveRecord::RecordInvalid` if the record is invalid.
|
1094
|
+
|
1068
1095
|
#### Options for `has_one`
|
1069
1096
|
|
1070
1097
|
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 +1144,12 @@ Controls what happens to the associated object when its owner is destroyed:
|
|
1117
1144
|
* `:restrict_with_exception` causes an exception to be raised if there is an associated record
|
1118
1145
|
* `:restrict_with_error` causes an error to be added to the owner if there is an associated object
|
1119
1146
|
|
1147
|
+
It's necessary not to set or leave `:nullify` option for those associations
|
1148
|
+
that have `NOT NULL` database constraints. If you don't set `dependent` to
|
1149
|
+
destroy such associations you won't be able to change the associated object
|
1150
|
+
because initial associated object foreign key will be set to unallowed `NULL`
|
1151
|
+
value.
|
1152
|
+
|
1120
1153
|
##### `:foreign_key`
|
1121
1154
|
|
1122
1155
|
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 +1293,7 @@ The `has_many` association creates a one-to-many relationship with another model
|
|
1260
1293
|
|
1261
1294
|
#### Methods Added by `has_many`
|
1262
1295
|
|
1263
|
-
When you declare a `has_many` association, the declaring class automatically gains
|
1296
|
+
When you declare a `has_many` association, the declaring class automatically gains 16 methods related to the association:
|
1264
1297
|
|
1265
1298
|
* `collection(force_reload = false)`
|
1266
1299
|
* `collection<<(object, ...)`
|
@@ -1277,6 +1310,7 @@ When you declare a `has_many` association, the declaring class automatically gai
|
|
1277
1310
|
* `collection.exists?(...)`
|
1278
1311
|
* `collection.build(attributes = {}, ...)`
|
1279
1312
|
* `collection.create(attributes = {})`
|
1313
|
+
* `collection.create!(attributes = {})`
|
1280
1314
|
|
1281
1315
|
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
1316
|
|
@@ -1304,6 +1338,7 @@ orders.where(...)
|
|
1304
1338
|
orders.exists?(...)
|
1305
1339
|
orders.build(attributes = {}, ...)
|
1306
1340
|
orders.create(attributes = {})
|
1341
|
+
orders.create!(attributes = {})
|
1307
1342
|
```
|
1308
1343
|
|
1309
1344
|
##### `collection(force_reload = false)`
|
@@ -1419,6 +1454,10 @@ The `collection.create` method returns a new object of the associated type. This
|
|
1419
1454
|
order_number: "A12345")
|
1420
1455
|
```
|
1421
1456
|
|
1457
|
+
##### `collection.create!(attributes = {})`
|
1458
|
+
|
1459
|
+
Does the same as `collection.create` above, but raises `ActiveRecord::RecordInvalid` if the record is invalid.
|
1460
|
+
|
1422
1461
|
#### Options for `has_many`
|
1423
1462
|
|
1424
1463
|
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 +1542,20 @@ end
|
|
1503
1542
|
|
1504
1543
|
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
1544
|
|
1545
|
+
Let's say that `users` table has `id` as the primary_key but it also has
|
1546
|
+
`guid` column. And the requirement is that `todos` table should hold
|
1547
|
+
`guid` column value and not `id` value. This can be achieved like this
|
1548
|
+
|
1549
|
+
```ruby
|
1550
|
+
class User < ActiveRecord::Base
|
1551
|
+
has_many :todos, primary_key: :guid
|
1552
|
+
end
|
1553
|
+
```
|
1554
|
+
|
1555
|
+
Now if we execute `@user.todos.create` then `@todo` record will have
|
1556
|
+
`user_id` value as the `guid` value of `@user`.
|
1557
|
+
|
1558
|
+
|
1506
1559
|
##### `:source`
|
1507
1560
|
|
1508
1561
|
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 +1723,7 @@ person.posts.inspect # => [#<Post id: 5, name: "a1">, #<Post id: 5, name: "a1">]
|
|
1670
1723
|
Reading.all.inspect # => [#<Reading id: 12, person_id: 5, post_id: 5>, #<Reading id: 13, person_id: 5, post_id: 5>]
|
1671
1724
|
```
|
1672
1725
|
|
1673
|
-
In the above case there are two readings and `person.posts` brings out both of
|
1726
|
+
In the above case there are two readings and `person.posts` brings out both of
|
1674
1727
|
them even though these records are pointing to the same post.
|
1675
1728
|
|
1676
1729
|
Now let's set `distinct`:
|
@@ -1689,24 +1742,24 @@ person.posts.inspect # => [#<Post id: 7, name: "a1">]
|
|
1689
1742
|
Reading.all.inspect # => [#<Reading id: 16, person_id: 7, post_id: 7>, #<Reading id: 17, person_id: 7, post_id: 7>]
|
1690
1743
|
```
|
1691
1744
|
|
1692
|
-
In the above case there are still two readings. However `person.posts` shows
|
1745
|
+
In the above case there are still two readings. However `person.posts` shows
|
1693
1746
|
only one post because the collection loads only unique records.
|
1694
1747
|
|
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
|
-
|
1748
|
+
If you want to make sure that, upon insertion, all of the records in the
|
1749
|
+
persisted association are distinct (so that you can be sure that when you
|
1750
|
+
inspect the association that you will never find duplicate records), you should
|
1751
|
+
add a unique index on the table itself. For example, if you have a table named
|
1752
|
+
`person_posts` and you want to make sure all the posts are unique, you could
|
1700
1753
|
add the following in a migration:
|
1701
1754
|
|
1702
1755
|
```ruby
|
1703
|
-
add_index :person_posts, :post, :
|
1756
|
+
add_index :person_posts, :post, unique: true
|
1704
1757
|
```
|
1705
1758
|
|
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
|
1759
|
+
Note that checking for uniqueness using something like `include?` is subject
|
1760
|
+
to race conditions. Do not attempt to use `include?` to enforce distinctness
|
1761
|
+
in an association. For instance, using the post example from above, the
|
1762
|
+
following code would be racy because multiple users could be attempting this
|
1710
1763
|
at the same time:
|
1711
1764
|
|
1712
1765
|
```ruby
|
@@ -1729,7 +1782,7 @@ The `has_and_belongs_to_many` association creates a many-to-many relationship wi
|
|
1729
1782
|
|
1730
1783
|
#### Methods Added by `has_and_belongs_to_many`
|
1731
1784
|
|
1732
|
-
When you declare a `has_and_belongs_to_many` association, the declaring class automatically gains
|
1785
|
+
When you declare a `has_and_belongs_to_many` association, the declaring class automatically gains 16 methods related to the association:
|
1733
1786
|
|
1734
1787
|
* `collection(force_reload = false)`
|
1735
1788
|
* `collection<<(object, ...)`
|
@@ -1746,6 +1799,7 @@ When you declare a `has_and_belongs_to_many` association, the declaring class au
|
|
1746
1799
|
* `collection.exists?(...)`
|
1747
1800
|
* `collection.build(attributes = {})`
|
1748
1801
|
* `collection.create(attributes = {})`
|
1802
|
+
* `collection.create!(attributes = {})`
|
1749
1803
|
|
1750
1804
|
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
1805
|
|
@@ -1773,6 +1827,7 @@ assemblies.where(...)
|
|
1773
1827
|
assemblies.exists?(...)
|
1774
1828
|
assemblies.build(attributes = {}, ...)
|
1775
1829
|
assemblies.create(attributes = {})
|
1830
|
+
assemblies.create!(attributes = {})
|
1776
1831
|
```
|
1777
1832
|
|
1778
1833
|
##### Additional Column Methods
|
@@ -1892,6 +1947,10 @@ The `collection.create` method returns a new object of the associated type. This
|
|
1892
1947
|
@assembly = @part.assemblies.create({assembly_name: "Transmission housing"})
|
1893
1948
|
```
|
1894
1949
|
|
1950
|
+
##### `collection.create!(attributes = {})`
|
1951
|
+
|
1952
|
+
Does the same as `collection.create`, but raises `ActiveRecord::RecordInvalid` if the record is invalid.
|
1953
|
+
|
1895
1954
|
#### Options for `has_and_belongs_to_many`
|
1896
1955
|
|
1897
1956
|
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 +1980,7 @@ TIP: The `:foreign_key` and `:association_foreign_key` options are useful when s
|
|
1921
1980
|
|
1922
1981
|
```ruby
|
1923
1982
|
class User < ActiveRecord::Base
|
1924
|
-
has_and_belongs_to_many :friends,
|
1983
|
+
has_and_belongs_to_many :friends,
|
1925
1984
|
class_name: "User",
|
1926
1985
|
foreign_key: "this_user_id",
|
1927
1986
|
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
|
@@ -1,8 +1,6 @@
|
|
1
1
|
The Rails Command Line
|
2
2
|
======================
|
3
3
|
|
4
|
-
Rails comes with every command line tool you'll need to
|
5
|
-
|
6
4
|
After reading this guide, you will know:
|
7
5
|
|
8
6
|
* How to create a Rails application.
|
@@ -27,6 +25,8 @@ There are a few commands that are absolutely critical to your everyday usage of
|
|
27
25
|
* `rails dbconsole`
|
28
26
|
* `rails new app_name`
|
29
27
|
|
28
|
+
All commands can run with ```-h or --help``` to list more information.
|
29
|
+
|
30
30
|
Let's create a simple Rails application to step through each of these commands in context.
|
31
31
|
|
32
32
|
### `rails new`
|
@@ -56,8 +56,6 @@ Rails will set you up with what seems like a huge amount of stuff for such a tin
|
|
56
56
|
|
57
57
|
The `rails server` command launches a small web server named WEBrick which comes bundled with Ruby. You'll use this any time you want to access your application through a web browser.
|
58
58
|
|
59
|
-
INFO: WEBrick isn't your only option for serving Rails. We'll get to that [later](#server-with-different-backends).
|
60
|
-
|
61
59
|
With no further work, `rails server` will run our new shiny Rails app:
|
62
60
|
|
63
61
|
```bash
|
@@ -67,9 +65,9 @@ $ rails server
|
|
67
65
|
=> Rails 4.0.0 application starting in development on http://0.0.0.0:3000
|
68
66
|
=> Call with -d to detach
|
69
67
|
=> Ctrl-C to shutdown server
|
70
|
-
[
|
71
|
-
[
|
72
|
-
[
|
68
|
+
[2013-08-07 02:00:01] INFO WEBrick 1.3.1
|
69
|
+
[2013-08-07 02:00:01] INFO ruby 2.0.0 (2013-06-27) [x86_64-darwin11.2.0]
|
70
|
+
[2013-08-07 02:00:01] INFO WEBrick::HTTPServer#start: pid=69680 port=3000
|
73
71
|
```
|
74
72
|
|
75
73
|
With just three commands we whipped up a Rails server listening on port 3000. Go to your browser and open [http://localhost:3000](http://localhost:3000), you will see a basic Rails app running.
|
@@ -220,7 +218,7 @@ We will set up a simple resource called "HighScore" that will keep track of our
|
|
220
218
|
```bash
|
221
219
|
$ rails generate scaffold HighScore game:string score:integer
|
222
220
|
invoke active_record
|
223
|
-
create db/migrate/
|
221
|
+
create db/migrate/20130717151933_create_high_scores.rb
|
224
222
|
create app/models/high_score.rb
|
225
223
|
invoke test_unit
|
226
224
|
create test/models/high_score_test.rb
|
@@ -242,18 +240,21 @@ $ rails generate scaffold HighScore game:string score:integer
|
|
242
240
|
create app/helpers/high_scores_helper.rb
|
243
241
|
invoke test_unit
|
244
242
|
create test/helpers/high_scores_helper_test.rb
|
243
|
+
invoke jbuilder
|
244
|
+
create app/views/high_scores/index.json.jbuilder
|
245
|
+
create app/views/high_scores/show.json.jbuilder
|
245
246
|
invoke assets
|
246
247
|
invoke coffee
|
247
248
|
create app/assets/javascripts/high_scores.js.coffee
|
248
249
|
invoke scss
|
249
250
|
create app/assets/stylesheets/high_scores.css.scss
|
250
251
|
invoke scss
|
251
|
-
|
252
|
+
identical app/assets/stylesheets/scaffolds.css.scss
|
252
253
|
```
|
253
254
|
|
254
255
|
The generator checks that there exist the directories for models, controllers, helpers, layouts, functional and unit tests, stylesheets, creates the views, controller, model and database migration for HighScore (creating the `high_scores` table and fields), takes care of the route for the **resource**, and new tests for everything.
|
255
256
|
|
256
|
-
The migration requires that we **migrate**, that is, run some Ruby code (living in that `
|
257
|
+
The migration requires that we **migrate**, that is, run some Ruby code (living in that `20130717151933_create_high_scores.rb`) to modify the schema of our database. Which database? The sqlite3 database that Rails will create for you when we run the `rake db:migrate` command. We'll talk more about Rake in-depth in a little while.
|
257
258
|
|
258
259
|
```bash
|
259
260
|
$ rake db:migrate
|
@@ -348,6 +349,9 @@ Rake is Ruby Make, a standalone Ruby utility that replaces the Unix utility 'mak
|
|
348
349
|
|
349
350
|
You can get a list of Rake tasks available to you, which will often depend on your current directory, by typing `rake --tasks`. Each task has a description, and should help you find the thing you need.
|
350
351
|
|
352
|
+
To get the full backtrace for running rake task you can pass the option
|
353
|
+
```--trace``` to command line, for example ```rake db:create --trace```.
|
354
|
+
|
351
355
|
```bash
|
352
356
|
$ rake --tasks
|
353
357
|
rake about # List versions of all Rails frameworks and the environment
|
@@ -361,6 +365,7 @@ rake middleware # Prints out your Rack middleware stack
|
|
361
365
|
rake tmp:clear # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tmp:sockets:clear)
|
362
366
|
rake tmp:create # Creates tmp directories for sessions, cache, sockets, and pids
|
363
367
|
```
|
368
|
+
INFO: You can also use ```rake -T``` to get the list of tasks.
|
364
369
|
|
365
370
|
### `about`
|
366
371
|
|
@@ -372,13 +377,14 @@ About your application's environment
|
|
372
377
|
Ruby version 1.9.3 (x86_64-linux)
|
373
378
|
RubyGems version 1.3.6
|
374
379
|
Rack version 1.3
|
375
|
-
Rails version 4.
|
380
|
+
Rails version 4.1.0
|
376
381
|
JavaScript Runtime Node.js (V8)
|
377
|
-
Active Record version 4.
|
378
|
-
Action Pack version 4.
|
379
|
-
Action
|
380
|
-
|
381
|
-
|
382
|
+
Active Record version 4.1.0
|
383
|
+
Action Pack version 4.1.0
|
384
|
+
Action View version 4.1.0
|
385
|
+
Action Mailer version 4.1.0
|
386
|
+
Active Support version 4.1.0
|
387
|
+
Middleware Rack::Sendfile, ActionDispatch::Static, Rack::Lock, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007ffd131a7c88>, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::Migration::CheckPending, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, Rack::Head, Rack::ConditionalGet, Rack::ETag
|
382
388
|
Application root /home/foobar/commandsapp
|
383
389
|
Environment development
|
384
390
|
Database adapter sqlite3
|
@@ -462,18 +468,19 @@ spec/models/user_spec.rb:
|
|
462
468
|
|
463
469
|
INFO: A good description of unit testing in Rails is given in [A Guide to Testing Rails Applications](testing.html)
|
464
470
|
|
465
|
-
Rails comes with a test suite called
|
471
|
+
Rails comes with a test suite called Minitest. Rails owes its stability to the use of tests. The tasks available in the `test:` namespace helps in running the different tests you will hopefully write.
|
466
472
|
|
467
473
|
### `tmp`
|
468
474
|
|
469
475
|
The `Rails.root/tmp` directory is, like the *nix /tmp directory, the holding place for temporary files like sessions (if you're using a file store for files), process id files, and cached actions.
|
470
476
|
|
471
|
-
The `tmp:` namespaced tasks will help you clear the `Rails.root/tmp` directory:
|
477
|
+
The `tmp:` namespaced tasks will help you clear and create the `Rails.root/tmp` directory:
|
472
478
|
|
473
479
|
* `rake tmp:cache:clear` clears `tmp/cache`.
|
474
480
|
* `rake tmp:sessions:clear` clears `tmp/sessions`.
|
475
481
|
* `rake tmp:sockets:clear` clears `tmp/sockets`.
|
476
482
|
* `rake tmp:clear` clears all the three: cache, sessions and sockets.
|
483
|
+
* `rake tmp:create` creates tmp directories for sessions, cache, sockets, and pids.
|
477
484
|
|
478
485
|
### Miscellaneous
|
479
486
|
|
@@ -483,7 +490,9 @@ The `tmp:` namespaced tasks will help you clear the `Rails.root/tmp` directory:
|
|
483
490
|
|
484
491
|
### Custom Rake Tasks
|
485
492
|
|
486
|
-
Custom rake tasks have a `.rake` extension and are placed in
|
493
|
+
Custom rake tasks have a `.rake` extension and are placed in
|
494
|
+
`Rails.root/lib/tasks`. You can create these custom rake tasks with the
|
495
|
+
`bin/rails generate task` command.
|
487
496
|
|
488
497
|
```ruby
|
489
498
|
desc "I am short, but comprehensive description for my cool task"
|