rails 4.1.16 → 4.2.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/guides/CHANGELOG.md +13 -102
- data/guides/Rakefile +2 -2
- data/guides/assets/javascripts/guides.js +6 -0
- data/guides/assets/stylesheets/main.css +4 -1
- data/guides/bug_report_templates/action_controller_gem.rb +2 -2
- data/guides/bug_report_templates/action_controller_master.rb +5 -2
- data/guides/bug_report_templates/active_record_master.rb +2 -0
- data/guides/rails_guides.rb +2 -2
- data/guides/rails_guides/helpers.rb +1 -1
- data/guides/rails_guides/levenshtein.rb +29 -21
- data/guides/rails_guides/markdown.rb +6 -7
- data/guides/rails_guides/markdown/renderer.rb +1 -1
- data/guides/source/2_3_release_notes.md +3 -3
- data/guides/source/3_0_release_notes.md +4 -4
- data/guides/source/3_1_release_notes.md +2 -2
- data/guides/source/3_2_release_notes.md +2 -2
- data/guides/source/4_1_release_notes.md +8 -9
- data/guides/source/4_2_release_notes.md +572 -0
- data/guides/source/_license.html.erb +1 -1
- data/guides/source/_welcome.html.erb +2 -8
- data/guides/source/action_controller_overview.md +79 -7
- data/guides/source/action_mailer_basics.md +36 -11
- data/guides/source/action_view_overview.md +138 -119
- data/guides/source/active_job_basics.md +253 -0
- data/guides/source/active_model_basics.md +23 -0
- data/guides/source/active_record_basics.md +16 -15
- data/guides/source/active_record_callbacks.md +12 -9
- data/guides/source/{migrations.md → active_record_migrations.md} +90 -217
- data/guides/source/active_record_postgresql.md +437 -0
- data/guides/source/active_record_querying.md +261 -261
- data/guides/source/active_record_validations.md +7 -7
- data/guides/source/active_support_core_extensions.md +105 -44
- data/guides/source/active_support_instrumentation.md +3 -2
- data/guides/source/api_documentation_guidelines.md +62 -16
- data/guides/source/asset_pipeline.md +58 -46
- data/guides/source/association_basics.md +47 -38
- data/guides/source/caching_with_rails.md +31 -6
- data/guides/source/command_line.md +56 -25
- data/guides/source/configuring.md +98 -19
- data/guides/source/contributing_to_ruby_on_rails.md +174 -111
- data/guides/source/credits.html.erb +1 -1
- data/guides/source/debugging_rails_applications.md +438 -284
- data/guides/source/development_dependencies_install.md +17 -4
- data/guides/source/documents.yaml +11 -7
- data/guides/source/engines.md +192 -203
- data/guides/source/form_helpers.md +54 -45
- data/guides/source/generators.md +20 -11
- data/guides/source/getting_started.md +330 -191
- data/guides/source/i18n.md +92 -62
- data/guides/source/index.html.erb +1 -0
- data/guides/source/initialization.md +108 -59
- data/guides/source/layout.html.erb +1 -4
- data/guides/source/layouts_and_rendering.md +24 -23
- data/guides/source/nested_model_forms.md +3 -3
- data/guides/source/plugins.md +26 -26
- data/guides/source/rails_application_templates.md +21 -3
- data/guides/source/rails_on_rack.md +1 -1
- data/guides/source/routing.md +97 -71
- data/guides/source/ruby_on_rails_guides_guidelines.md +10 -12
- data/guides/source/security.md +39 -33
- data/guides/source/testing.md +111 -108
- data/guides/source/upgrading_ruby_on_rails.md +131 -14
- data/guides/source/working_with_javascript_in_rails.md +18 -16
- data/guides/w3c_validator.rb +2 -0
- metadata +37 -94
- data/guides/bug_report_templates/generic_gem.rb +0 -15
- data/guides/bug_report_templates/generic_master.rb +0 -26
- data/guides/code/getting_started/Gemfile +0 -40
- data/guides/code/getting_started/Gemfile.lock +0 -125
- data/guides/code/getting_started/README.rdoc +0 -28
- data/guides/code/getting_started/Rakefile +0 -6
- data/guides/code/getting_started/app/assets/javascripts/application.js +0 -15
- data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/application.css +0 -13
- data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +0 -3
- data/guides/code/getting_started/app/controllers/application_controller.rb +0 -5
- data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -23
- data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -53
- data/guides/code/getting_started/app/controllers/welcome_controller.rb +0 -4
- data/guides/code/getting_started/app/helpers/application_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/comments_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/posts_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/welcome_helper.rb +0 -2
- data/guides/code/getting_started/app/models/comment.rb +0 -3
- data/guides/code/getting_started/app/models/post.rb +0 -7
- data/guides/code/getting_started/app/views/comments/_comment.html.erb +0 -15
- data/guides/code/getting_started/app/views/comments/_form.html.erb +0 -13
- data/guides/code/getting_started/app/views/layouts/application.html.erb +0 -14
- data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -27
- data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/index.html.erb +0 -21
- data/guides/code/getting_started/app/views/posts/new.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/show.html.erb +0 -18
- data/guides/code/getting_started/app/views/welcome/index.html.erb +0 -4
- data/guides/code/getting_started/bin/bundle +0 -4
- data/guides/code/getting_started/bin/rails +0 -4
- data/guides/code/getting_started/bin/rake +0 -4
- data/guides/code/getting_started/config.ru +0 -4
- data/guides/code/getting_started/config/application.rb +0 -18
- data/guides/code/getting_started/config/boot.rb +0 -4
- data/guides/code/getting_started/config/database.yml +0 -25
- data/guides/code/getting_started/config/environment.rb +0 -5
- data/guides/code/getting_started/config/environments/development.rb +0 -30
- data/guides/code/getting_started/config/environments/production.rb +0 -80
- data/guides/code/getting_started/config/environments/test.rb +0 -36
- data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
- data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +0 -4
- data/guides/code/getting_started/config/initializers/inflections.rb +0 -16
- data/guides/code/getting_started/config/initializers/locale.rb +0 -9
- data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
- data/guides/code/getting_started/config/initializers/secret_token.rb +0 -12
- data/guides/code/getting_started/config/initializers/session_store.rb +0 -3
- data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
- data/guides/code/getting_started/config/locales/en.yml +0 -23
- data/guides/code/getting_started/config/routes.rb +0 -7
- data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +0 -10
- data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +0 -11
- data/guides/code/getting_started/db/schema.rb +0 -33
- data/guides/code/getting_started/db/seeds.rb +0 -7
- data/guides/code/getting_started/public/404.html +0 -60
- data/guides/code/getting_started/public/422.html +0 -60
- data/guides/code/getting_started/public/500.html +0 -59
- data/guides/code/getting_started/public/favicon.ico +0 -0
- data/guides/code/getting_started/public/robots.txt +0 -5
- data/guides/code/getting_started/test/controllers/comments_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/posts_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +0 -9
- data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
- data/guides/code/getting_started/test/fixtures/posts.yml +0 -9
- data/guides/code/getting_started/test/helpers/comments_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/posts_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +0 -4
- data/guides/code/getting_started/test/models/comment_test.rb +0 -7
- data/guides/code/getting_started/test/models/post_test.rb +0 -7
- data/guides/code/getting_started/test/test_helper.rb +0 -12
@@ -56,11 +56,11 @@ the comment operator on that line to later enable the asset pipeline:
|
|
56
56
|
|
57
57
|
To set asset compression methods, set the appropriate configuration options
|
58
58
|
in `production.rb` - `config.assets.css_compressor` for your CSS and
|
59
|
-
`config.assets.js_compressor` for your
|
59
|
+
`config.assets.js_compressor` for your JavaScript:
|
60
60
|
|
61
61
|
```ruby
|
62
62
|
config.assets.css_compressor = :yui
|
63
|
-
config.assets.js_compressor = :
|
63
|
+
config.assets.js_compressor = :uglifier
|
64
64
|
```
|
65
65
|
|
66
66
|
NOTE: The `sass-rails` gem is automatically used for CSS compression if included
|
@@ -124,19 +124,22 @@ with a built-in helper. In the source the generated code looked like this:
|
|
124
124
|
The query string strategy has several disadvantages:
|
125
125
|
|
126
126
|
1. **Not all caches will reliably cache content where the filename only differs by
|
127
|
-
query parameters
|
127
|
+
query parameters**
|
128
|
+
|
128
129
|
[Steve Souders recommends](http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/),
|
129
130
|
"...avoiding a querystring for cacheable resources". He found that in this
|
130
131
|
case 5-20% of requests will not be cached. Query strings in particular do not
|
131
132
|
work at all with some CDNs for cache invalidation.
|
132
133
|
|
133
|
-
2. **The file name can change between nodes in multi-server environments
|
134
|
+
2. **The file name can change between nodes in multi-server environments.**
|
135
|
+
|
134
136
|
The default query string in Rails 2.x is based on the modification time of
|
135
137
|
the files. When assets are deployed to a cluster, there is no guarantee that the
|
136
138
|
timestamps will be the same, resulting in different values being used depending
|
137
139
|
on which server handles the request.
|
138
140
|
|
139
|
-
3. **Too much cache invalidation
|
141
|
+
3. **Too much cache invalidation**
|
142
|
+
|
140
143
|
When static assets are deployed with each new release of code, the mtime
|
141
144
|
(time of last modification) of _all_ these files changes, forcing all remote
|
142
145
|
clients to fetch them again, even when the content of those assets has not changed.
|
@@ -198,17 +201,14 @@ will result in your assets being included more than once.
|
|
198
201
|
|
199
202
|
WARNING: When using asset precompilation, you will need to ensure that your
|
200
203
|
controller assets will be precompiled when loading them on a per page basis. By
|
201
|
-
default .coffee and .scss files will not be precompiled on their own.
|
202
|
-
|
203
|
-
|
204
|
-
production, however, you will see 500 errors since live compilation is turned
|
205
|
-
off by default. See [Precompiling Assets](#precompiling-assets) for more
|
206
|
-
information on how precompiling works.
|
204
|
+
default .coffee and .scss files will not be precompiled on their own. See
|
205
|
+
[Precompiling Assets](#precompiling-assets) for more information on how
|
206
|
+
precompiling works.
|
207
207
|
|
208
208
|
NOTE: You must have an ExecJS supported runtime in order to use CoffeeScript.
|
209
209
|
If you are using Mac OS X or Windows, you have a JavaScript runtime installed in
|
210
210
|
your operating system. Check
|
211
|
-
[ExecJS](https://github.com/
|
211
|
+
[ExecJS](https://github.com/sstephenson/execjs#readme) documentation to know all
|
212
212
|
supported JavaScript runtimes.
|
213
213
|
|
214
214
|
You can also disable generation of controller specific asset files by adding the
|
@@ -302,7 +302,7 @@ Sprockets uses files named `index` (with the relevant extensions) for a special
|
|
302
302
|
purpose.
|
303
303
|
|
304
304
|
For example, if you have a jQuery library with many modules, which is stored in
|
305
|
-
`lib/assets/library_name`, the file `lib/assets/library_name/index.js` serves as
|
305
|
+
`lib/assets/javascripts/library_name`, the file `lib/assets/javascripts/library_name/index.js` serves as
|
306
306
|
the manifest for all files in this library. This file could include a list of
|
307
307
|
all the required files in order, or a simple `require_tree` directive.
|
308
308
|
|
@@ -493,14 +493,13 @@ The directives that work in JavaScript files also work in stylesheets
|
|
493
493
|
one, requiring all stylesheets from the current directory.
|
494
494
|
|
495
495
|
In this example, `require_self` is used. This puts the CSS contained within the
|
496
|
-
file (if any) at the precise location of the `require_self` call.
|
497
|
-
`require_self` is called more than once, only the last call is respected.
|
496
|
+
file (if any) at the precise location of the `require_self` call.
|
498
497
|
|
499
498
|
NOTE. If you want to use multiple Sass files, you should generally use the [Sass `@import` rule](http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#import)
|
500
|
-
instead of these Sprockets directives.
|
499
|
+
instead of these Sprockets directives. When using Sprockets directives, Sass files exist within
|
501
500
|
their own scope, making variables or mixins only available within the document they were defined in.
|
502
|
-
|
503
|
-
equivalent to how `require_tree` works. Check the [sass-rails documentation](https://github.com/rails/sass-rails#features) for more info and important caveats.
|
501
|
+
|
502
|
+
You can do file globbing as well using `@import "*"`, and `@import "**/*"` to add the whole tree which is equivalent to how `require_tree` works. Check the [sass-rails documentation](https://github.com/rails/sass-rails#features) for more info and important caveats.
|
504
503
|
|
505
504
|
You can have as many manifest files as you need. For example, the `admin.css`
|
506
505
|
and `admin.js` manifest could contain the JS and CSS files that are used for the
|
@@ -581,23 +580,21 @@ runtime. To disable this behavior you can set:
|
|
581
580
|
config.assets.raise_runtime_errors = false
|
582
581
|
```
|
583
582
|
|
584
|
-
When
|
583
|
+
When this option is true, the asset pipeline will check if all the assets loaded
|
584
|
+
in your application are included in the `config.assets.precompile` list.
|
585
|
+
If `config.assets.digest` is also true, the asset pipeline will require that
|
586
|
+
all requests for assets include digests.
|
585
587
|
|
586
|
-
|
588
|
+
### Turning Digests Off
|
587
589
|
|
588
|
-
|
589
|
-
|
590
|
-
```
|
591
|
-
|
592
|
-
Then you must declare that `logo.png` is a dependency of `application.css.erb`, so when the image gets re-compiled, the css file does as well. You can do this using the `//= depend_on_asset` declaration:
|
590
|
+
You can turn off digests by updating `config/environments/development.rb` to
|
591
|
+
include:
|
593
592
|
|
594
|
-
```
|
595
|
-
|
596
|
-
#logo { background: url(<%= asset_data_uri 'logo.png' %>) }
|
593
|
+
```ruby
|
594
|
+
config.assets.digest = false
|
597
595
|
```
|
598
596
|
|
599
|
-
|
600
|
-
|
597
|
+
When this option is true, digests will be generated for asset URLs.
|
601
598
|
|
602
599
|
### Turning Debugging Off
|
603
600
|
|
@@ -714,7 +711,7 @@ The default matcher for compiling files includes `application.js`,
|
|
714
711
|
automatically) from `app/assets` folders including your gems:
|
715
712
|
|
716
713
|
```ruby
|
717
|
-
[ Proc.new { |
|
714
|
+
[ Proc.new { |filename, path| path =~ /app\/assets/ && !%w(.js .css).include?(File.extname(filename)) },
|
718
715
|
/application.(css|js)$/ ]
|
719
716
|
```
|
720
717
|
|
@@ -781,7 +778,7 @@ exception indicating the name of the missing file(s).
|
|
781
778
|
|
782
779
|
#### Far-future Expires Header
|
783
780
|
|
784
|
-
Precompiled assets exist on the
|
781
|
+
Precompiled assets exist on the file system and are served directly by your web
|
785
782
|
server. They do not have far-future headers by default, so to get the benefit of
|
786
783
|
fingerprinting you'll have to update your server configuration to add those
|
787
784
|
headers.
|
@@ -801,7 +798,7 @@ For Apache:
|
|
801
798
|
</Location>
|
802
799
|
```
|
803
800
|
|
804
|
-
For
|
801
|
+
For NGINX:
|
805
802
|
|
806
803
|
```nginx
|
807
804
|
location ~ ^/assets/ {
|
@@ -823,7 +820,7 @@ compression ratio, thus reducing the size of the data transfer to the minimum.
|
|
823
820
|
On the other hand, web servers can be configured to serve compressed content
|
824
821
|
directly from disk, rather than deflating non-compressed files themselves.
|
825
822
|
|
826
|
-
|
823
|
+
NGINX is able to do this automatically enabling `gzip_static`:
|
827
824
|
|
828
825
|
```nginx
|
829
826
|
location ~ ^/(assets)/ {
|
@@ -842,7 +839,7 @@ the module compiled. Otherwise, you may need to perform a manual compilation:
|
|
842
839
|
./configure --with-http_gzip_static_module
|
843
840
|
```
|
844
841
|
|
845
|
-
If you're compiling
|
842
|
+
If you're compiling NGINX with Phusion Passenger you'll need to pass that option
|
846
843
|
when prompted.
|
847
844
|
|
848
845
|
A robust configuration for Apache is possible but tricky; please Google around.
|
@@ -861,10 +858,12 @@ duplication of work.
|
|
861
858
|
Local compilation allows you to commit the compiled files into source control,
|
862
859
|
and deploy as normal.
|
863
860
|
|
864
|
-
There are
|
861
|
+
There are three caveats:
|
865
862
|
|
866
863
|
* You must not run the Capistrano deployment task that precompiles assets.
|
867
|
-
* You must
|
864
|
+
* You must ensure any necessary compressors or minifiers are
|
865
|
+
available on your development system.
|
866
|
+
* You must change the following application configuration setting:
|
868
867
|
|
869
868
|
In `config/environments/development.rb`, place the following line:
|
870
869
|
|
@@ -878,9 +877,6 @@ development mode, and pass all requests to Sprockets. The prefix is still set to
|
|
878
877
|
would serve the precompiled assets from `/assets` in development, and you would
|
879
878
|
not see any local changes until you compile assets again.
|
880
879
|
|
881
|
-
You will also need to ensure any necessary compressors or minifiers are
|
882
|
-
available on your development system.
|
883
|
-
|
884
880
|
In practice, this will allow you to precompile locally, have those files in your
|
885
881
|
working tree, and commit those files to source control when needed. Development
|
886
882
|
mode will work as expected.
|
@@ -927,9 +923,17 @@ cache forever. This can cause problems. If you use
|
|
927
923
|
|
928
924
|
Every cache is different, so evaluate how your CDN handles caching and make sure
|
929
925
|
that it plays nicely with the pipeline. You may find quirks related to your
|
930
|
-
specific set up, you may not. The defaults
|
926
|
+
specific set up, you may not. The defaults NGINX uses, for example, should give
|
931
927
|
you no problems when used as an HTTP cache.
|
932
928
|
|
929
|
+
If you want to serve only some assets from your CDN, you can use custom
|
930
|
+
`:host` option of `asset_url` helper, which overwrites value set in
|
931
|
+
`config.action_controller.asset_host`.
|
932
|
+
|
933
|
+
```ruby
|
934
|
+
asset_url 'image.png', :host => 'http://cdn.example.com'
|
935
|
+
```
|
936
|
+
|
933
937
|
Customizing the Pipeline
|
934
938
|
------------------------
|
935
939
|
|
@@ -969,7 +973,7 @@ The following line invokes `uglifier` for JavaScript compression.
|
|
969
973
|
config.assets.js_compressor = :uglifier
|
970
974
|
```
|
971
975
|
|
972
|
-
NOTE: You will need an [ExecJS](https://github.com/
|
976
|
+
NOTE: You will need an [ExecJS](https://github.com/sstephenson/execjs#readme)
|
973
977
|
supported runtime in order to use `uglifier`. If you are using Mac OS X or
|
974
978
|
Windows you have a JavaScript runtime installed in your operating system.
|
975
979
|
|
@@ -1023,12 +1027,12 @@ this passes responsibility for serving the file to the web server, which is
|
|
1023
1027
|
faster. Have a look at [send_file](http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_file)
|
1024
1028
|
on how to use this feature.
|
1025
1029
|
|
1026
|
-
Apache and
|
1030
|
+
Apache and NGINX support this option, which can be enabled in
|
1027
1031
|
`config/environments/production.rb`:
|
1028
1032
|
|
1029
1033
|
```ruby
|
1030
|
-
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for
|
1031
|
-
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for
|
1034
|
+
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache
|
1035
|
+
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
|
1032
1036
|
```
|
1033
1037
|
|
1034
1038
|
WARNING: If you are upgrading an existing application and intend to use this
|
@@ -1038,7 +1042,7 @@ and any other environments you define with production behavior (not
|
|
1038
1042
|
|
1039
1043
|
TIP: For further details have a look at the docs of your production web server:
|
1040
1044
|
- [Apache](https://tn123.org/mod_xsendfile/)
|
1041
|
-
- [
|
1045
|
+
- [NGINX](http://wiki.nginx.org/XSendfile)
|
1042
1046
|
|
1043
1047
|
Assets Cache Store
|
1044
1048
|
------------------
|
@@ -1058,6 +1062,14 @@ cache store.
|
|
1058
1062
|
config.assets.cache_store = :memory_store, { size: 32.megabytes }
|
1059
1063
|
```
|
1060
1064
|
|
1065
|
+
To disable the assets cache store:
|
1066
|
+
|
1067
|
+
```ruby
|
1068
|
+
config.assets.configure do |env|
|
1069
|
+
env.cache = ActiveSupport::Cache.lookup_store(:null_store)
|
1070
|
+
end
|
1071
|
+
```
|
1072
|
+
|
1061
1073
|
Adding Assets to Your Gems
|
1062
1074
|
--------------------------
|
1063
1075
|
|
@@ -105,7 +105,7 @@ class CreateOrders < ActiveRecord::Migration
|
|
105
105
|
end
|
106
106
|
|
107
107
|
create_table :orders do |t|
|
108
|
-
t.belongs_to :customer
|
108
|
+
t.belongs_to :customer, index: true
|
109
109
|
t.datetime :order_date
|
110
110
|
t.timestamps
|
111
111
|
end
|
@@ -136,7 +136,7 @@ class CreateSuppliers < ActiveRecord::Migration
|
|
136
136
|
end
|
137
137
|
|
138
138
|
create_table :accounts do |t|
|
139
|
-
t.belongs_to :supplier
|
139
|
+
t.belongs_to :supplier, index: true
|
140
140
|
t.string :account_number
|
141
141
|
t.timestamps
|
142
142
|
end
|
@@ -169,7 +169,7 @@ class CreateCustomers < ActiveRecord::Migration
|
|
169
169
|
end
|
170
170
|
|
171
171
|
create_table :orders do |t|
|
172
|
-
t.belongs_to :customer
|
172
|
+
t.belongs_to :customer, index:true
|
173
173
|
t.datetime :order_date
|
174
174
|
t.timestamps
|
175
175
|
end
|
@@ -216,8 +216,8 @@ class CreateAppointments < ActiveRecord::Migration
|
|
216
216
|
end
|
217
217
|
|
218
218
|
create_table :appointments do |t|
|
219
|
-
t.belongs_to :physician
|
220
|
-
t.belongs_to :patient
|
219
|
+
t.belongs_to :physician, index: true
|
220
|
+
t.belongs_to :patient, index: true
|
221
221
|
t.datetime :appointment_date
|
222
222
|
t.timestamps
|
223
223
|
end
|
@@ -295,13 +295,13 @@ class CreateAccountHistories < ActiveRecord::Migration
|
|
295
295
|
end
|
296
296
|
|
297
297
|
create_table :accounts do |t|
|
298
|
-
t.belongs_to :supplier
|
298
|
+
t.belongs_to :supplier, index: true
|
299
299
|
t.string :account_number
|
300
300
|
t.timestamps
|
301
301
|
end
|
302
302
|
|
303
303
|
create_table :account_histories do |t|
|
304
|
-
t.belongs_to :account
|
304
|
+
t.belongs_to :account, index: true
|
305
305
|
t.integer :credit_rating
|
306
306
|
t.timestamps
|
307
307
|
end
|
@@ -341,8 +341,8 @@ class CreateAssembliesAndParts < ActiveRecord::Migration
|
|
341
341
|
end
|
342
342
|
|
343
343
|
create_table :assemblies_parts, id: false do |t|
|
344
|
-
t.belongs_to :assembly
|
345
|
-
t.belongs_to :part
|
344
|
+
t.belongs_to :assembly, index: true
|
345
|
+
t.belongs_to :part, index: true
|
346
346
|
end
|
347
347
|
end
|
348
348
|
end
|
@@ -379,6 +379,8 @@ class CreateSuppliers < ActiveRecord::Migration
|
|
379
379
|
t.string :account_number
|
380
380
|
t.timestamps
|
381
381
|
end
|
382
|
+
|
383
|
+
add_index :accounts, :supplier_id
|
382
384
|
end
|
383
385
|
end
|
384
386
|
```
|
@@ -455,6 +457,8 @@ class CreatePictures < ActiveRecord::Migration
|
|
455
457
|
t.string :imageable_type
|
456
458
|
t.timestamps
|
457
459
|
end
|
460
|
+
|
461
|
+
add_index :pictures, :imageable_id
|
458
462
|
end
|
459
463
|
end
|
460
464
|
```
|
@@ -466,7 +470,7 @@ class CreatePictures < ActiveRecord::Migration
|
|
466
470
|
def change
|
467
471
|
create_table :pictures do |t|
|
468
472
|
t.string :name
|
469
|
-
t.references :imageable, polymorphic: true
|
473
|
+
t.references :imageable, polymorphic: true, index: true
|
470
474
|
t.timestamps
|
471
475
|
end
|
472
476
|
end
|
@@ -496,7 +500,7 @@ In your migrations/schema, you will add a references column to the model itself.
|
|
496
500
|
class CreateEmployees < ActiveRecord::Migration
|
497
501
|
def change
|
498
502
|
create_table :employees do |t|
|
499
|
-
t.references :manager
|
503
|
+
t.references :manager, index: true
|
500
504
|
t.timestamps
|
501
505
|
end
|
502
506
|
end
|
@@ -561,6 +565,8 @@ class CreateOrders < ActiveRecord::Migration
|
|
561
565
|
t.string :order_number
|
562
566
|
t.integer :customer_id
|
563
567
|
end
|
568
|
+
|
569
|
+
add_index :orders, :customer_id
|
564
570
|
end
|
565
571
|
end
|
566
572
|
```
|
@@ -571,7 +577,7 @@ If you create an association some time after you build the underlying model, you
|
|
571
577
|
|
572
578
|
If you create a `has_and_belongs_to_many` association, you need to explicitly create the joining table. Unless the name of the join table is explicitly specified by using the `:join_table` option, Active Record creates the name by using the lexical order of the class names. So a join between customer and order models will give the default join table name of "customers_orders" because "c" outranks "o" in lexical ordering.
|
573
579
|
|
574
|
-
WARNING: The precedence between model names is calculated using the `<` operator for `String`. This means that if the strings are of different lengths, and the strings are equal when compared up to the shortest length, then the longer string is considered of higher lexical precedence than the shorter one. For example, one would expect the tables "
|
580
|
+
WARNING: The precedence between model names is calculated using the `<` operator for `String`. This means that if the strings are of different lengths, and the strings are equal when compared up to the shortest length, then the longer string is considered of higher lexical precedence than the shorter one. For example, one would expect the tables "paper_boxes" and "papers" to generate a join table name of "papers_paper_boxes" because of the length of the name "paper_boxes", but it in fact generates a join table name of "paper_boxes_papers" (because the underscore '_' is lexicographically _less_ than 's' in common encodings).
|
575
581
|
|
576
582
|
Whatever the name, you must manually generate the join table with an appropriate migration. For example, consider these associations:
|
577
583
|
|
@@ -594,6 +600,9 @@ class CreateAssembliesPartsJoinTable < ActiveRecord::Migration
|
|
594
600
|
t.integer :assembly_id
|
595
601
|
t.integer :part_id
|
596
602
|
end
|
603
|
+
|
604
|
+
add_index :assemblies_parts, :assembly_id
|
605
|
+
add_index :assemblies_parts, :part_id
|
597
606
|
end
|
598
607
|
end
|
599
608
|
```
|
@@ -1131,7 +1140,7 @@ The `has_one` association supports these options:
|
|
1131
1140
|
|
1132
1141
|
##### `:as`
|
1133
1142
|
|
1134
|
-
Setting the `:as` option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail
|
1143
|
+
Setting the `:as` option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail [earlier in this guide](#polymorphic-associations).
|
1135
1144
|
|
1136
1145
|
##### `:autosave`
|
1137
1146
|
|
@@ -1203,7 +1212,7 @@ The `:source_type` option specifies the source association type for a `has_one :
|
|
1203
1212
|
|
1204
1213
|
##### `:through`
|
1205
1214
|
|
1206
|
-
The `:through` option specifies a join model through which to perform the query. `has_one :through` associations were discussed in detail
|
1215
|
+
The `:through` option specifies a join model through which to perform the query. `has_one :through` associations were discussed in detail [earlier in this guide](#the-has-one-through-association).
|
1207
1216
|
|
1208
1217
|
##### `:validate`
|
1209
1218
|
|
@@ -1497,7 +1506,7 @@ The `has_many` association supports these options:
|
|
1497
1506
|
|
1498
1507
|
##### `:as`
|
1499
1508
|
|
1500
|
-
Setting the `:as` option indicates that this is a polymorphic association, as discussed
|
1509
|
+
Setting the `:as` option indicates that this is a polymorphic association, as discussed [earlier in this guide](#polymorphic-associations).
|
1501
1510
|
|
1502
1511
|
##### `:autosave`
|
1503
1512
|
|
@@ -1579,7 +1588,7 @@ The `:source_type` option specifies the source association type for a `has_many
|
|
1579
1588
|
|
1580
1589
|
##### `:through`
|
1581
1590
|
|
1582
|
-
The `:through` option specifies a join model through which to perform the query. `has_many :through` associations provide a way to implement many-to-many relationships, as discussed
|
1591
|
+
The `:through` option specifies a join model through which to perform the query. `has_many :through` associations provide a way to implement many-to-many relationships, as discussed [earlier in this guide](#the-has-many-through-association).
|
1583
1592
|
|
1584
1593
|
##### `:validate`
|
1585
1594
|
|
@@ -1632,7 +1641,7 @@ If you use a hash-style `where` option, then record creation via this associatio
|
|
1632
1641
|
|
1633
1642
|
##### `extending`
|
1634
1643
|
|
1635
|
-
The `extending` method specifies a named module to extend the association proxy. Association extensions are discussed in detail
|
1644
|
+
The `extending` method specifies a named module to extend the association proxy. Association extensions are discussed in detail [later in this guide](#association-extensions).
|
1636
1645
|
|
1637
1646
|
##### `group`
|
1638
1647
|
|
@@ -1725,58 +1734,58 @@ mostly useful together with the `:through` option.
|
|
1725
1734
|
```ruby
|
1726
1735
|
class Person < ActiveRecord::Base
|
1727
1736
|
has_many :readings
|
1728
|
-
has_many :
|
1737
|
+
has_many :articles, through: :readings
|
1729
1738
|
end
|
1730
1739
|
|
1731
1740
|
person = Person.create(name: 'John')
|
1732
|
-
|
1733
|
-
person.
|
1734
|
-
person.
|
1735
|
-
person.
|
1736
|
-
Reading.all.inspect # => [#<Reading id: 12, person_id: 5,
|
1741
|
+
article = Article.create(name: 'a1')
|
1742
|
+
person.articles << article
|
1743
|
+
person.articles << article
|
1744
|
+
person.articles.inspect # => [#<Article id: 5, name: "a1">, #<Article id: 5, name: "a1">]
|
1745
|
+
Reading.all.inspect # => [#<Reading id: 12, person_id: 5, article_id: 5>, #<Reading id: 13, person_id: 5, article_id: 5>]
|
1737
1746
|
```
|
1738
1747
|
|
1739
|
-
In the above case there are two readings and `person.
|
1740
|
-
them even though these records are pointing to the same
|
1748
|
+
In the above case there are two readings and `person.articles` brings out both of
|
1749
|
+
them even though these records are pointing to the same article.
|
1741
1750
|
|
1742
1751
|
Now let's set `distinct`:
|
1743
1752
|
|
1744
1753
|
```ruby
|
1745
1754
|
class Person
|
1746
1755
|
has_many :readings
|
1747
|
-
has_many :
|
1756
|
+
has_many :articles, -> { distinct }, through: :readings
|
1748
1757
|
end
|
1749
1758
|
|
1750
1759
|
person = Person.create(name: 'Honda')
|
1751
|
-
|
1752
|
-
person.
|
1753
|
-
person.
|
1754
|
-
person.
|
1755
|
-
Reading.all.inspect # => [#<Reading id: 16, person_id: 7,
|
1760
|
+
article = Article.create(name: 'a1')
|
1761
|
+
person.articles << article
|
1762
|
+
person.articles << article
|
1763
|
+
person.articles.inspect # => [#<Article id: 7, name: "a1">]
|
1764
|
+
Reading.all.inspect # => [#<Reading id: 16, person_id: 7, article_id: 7>, #<Reading id: 17, person_id: 7, article_id: 7>]
|
1756
1765
|
```
|
1757
1766
|
|
1758
|
-
In the above case there are still two readings. However `person.
|
1759
|
-
only one
|
1767
|
+
In the above case there are still two readings. However `person.articles` shows
|
1768
|
+
only one article because the collection loads only unique records.
|
1760
1769
|
|
1761
1770
|
If you want to make sure that, upon insertion, all of the records in the
|
1762
1771
|
persisted association are distinct (so that you can be sure that when you
|
1763
1772
|
inspect the association that you will never find duplicate records), you should
|
1764
1773
|
add a unique index on the table itself. For example, if you have a table named
|
1765
|
-
`
|
1774
|
+
`person_articles` and you want to make sure all the articles are unique, you could
|
1766
1775
|
add the following in a migration:
|
1767
1776
|
|
1768
1777
|
```ruby
|
1769
|
-
add_index :
|
1778
|
+
add_index :person_articles, :article, unique: true
|
1770
1779
|
```
|
1771
1780
|
|
1772
1781
|
Note that checking for uniqueness using something like `include?` is subject
|
1773
1782
|
to race conditions. Do not attempt to use `include?` to enforce distinctness
|
1774
|
-
in an association. For instance, using the
|
1783
|
+
in an association. For instance, using the article example from above, the
|
1775
1784
|
following code would be racy because multiple users could be attempting this
|
1776
1785
|
at the same time:
|
1777
1786
|
|
1778
1787
|
```ruby
|
1779
|
-
person.
|
1788
|
+
person.articles << article unless person.articles.include?(article)
|
1780
1789
|
```
|
1781
1790
|
|
1782
1791
|
#### When are Objects Saved?
|
@@ -2082,7 +2091,7 @@ If you use a hash-style `where`, then record creation via this association will
|
|
2082
2091
|
|
2083
2092
|
##### `extending`
|
2084
2093
|
|
2085
|
-
The `extending` method specifies a named module to extend the association proxy. Association extensions are discussed in detail
|
2094
|
+
The `extending` method specifies a named module to extend the association proxy. Association extensions are discussed in detail [later in this guide](#association-extensions).
|
2086
2095
|
|
2087
2096
|
##### `group`
|
2088
2097
|
|