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
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
<ul class="nav">
|
|
49
49
|
<li><a class="nav-item" href="index.html">Home</a></li>
|
|
50
50
|
<li class="guides-index guides-index-large">
|
|
51
|
-
<a href="index.html"
|
|
51
|
+
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Guides Index</a>
|
|
52
52
|
<div id="guides" class="clearfix" style="display: none;">
|
|
53
53
|
<hr />
|
|
54
54
|
<% ['L', 'R'].each do |position| %>
|
|
@@ -139,7 +139,7 @@
|
|
|
139
139
|
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
|
|
140
140
|
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
|
|
141
141
|
<script type="text/javascript">
|
|
142
|
-
SyntaxHighlighter.all()
|
|
142
|
+
SyntaxHighlighter.all();
|
|
143
143
|
$(guidesIndex.bind);
|
|
144
144
|
</script>
|
|
145
145
|
</body>
|
|
@@ -88,7 +88,7 @@ If we want to display the properties of all the books in our view, we can do so
|
|
|
88
88
|
<% end %>
|
|
89
89
|
</table>
|
|
90
90
|
|
|
91
|
-
<br
|
|
91
|
+
<br>
|
|
92
92
|
|
|
93
93
|
<%= link_to "New book", new_book_path %>
|
|
94
94
|
```
|
|
@@ -122,8 +122,7 @@ X-Runtime: 0.014297
|
|
|
122
122
|
Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
|
|
123
123
|
Cache-Control: no-cache
|
|
124
124
|
|
|
125
|
-
|
|
126
|
-
$
|
|
125
|
+
$
|
|
127
126
|
```
|
|
128
127
|
|
|
129
128
|
We see there is an empty response (no data after the `Cache-Control` line), but the request was successful because Rails has set the response to 200 OK. You can set the `:status` option on render to change this response. Rendering nothing can be useful for Ajax requests where all you want to send back to the browser is an acknowledgment that the request was completed.
|
|
@@ -137,7 +136,7 @@ If you want to render the view that corresponds to a different template within t
|
|
|
137
136
|
```ruby
|
|
138
137
|
def update
|
|
139
138
|
@book = Book.find(params[:id])
|
|
140
|
-
if @book.update(
|
|
139
|
+
if @book.update(book_params)
|
|
141
140
|
redirect_to(@book)
|
|
142
141
|
else
|
|
143
142
|
render "edit"
|
|
@@ -152,7 +151,7 @@ If you prefer, you can use a symbol instead of a string to specify the action to
|
|
|
152
151
|
```ruby
|
|
153
152
|
def update
|
|
154
153
|
@book = Book.find(params[:id])
|
|
155
|
-
if @book.update(
|
|
154
|
+
if @book.update(book_params)
|
|
156
155
|
redirect_to(@book)
|
|
157
156
|
else
|
|
158
157
|
render :edit
|
|
@@ -176,23 +175,22 @@ render template: "products/show"
|
|
|
176
175
|
|
|
177
176
|
#### Rendering an Arbitrary File
|
|
178
177
|
|
|
179
|
-
The `render` method can also use a view that's entirely outside of your application
|
|
180
|
-
|
|
181
|
-
```ruby
|
|
182
|
-
render "/u/apps/warehouse_app/current/app/views/products/show"
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
Rails determines that this is a file render because of the leading slash character. To be explicit, you can use the `:file` option (which was required on Rails 2.2 and earlier):
|
|
178
|
+
The `render` method can also use a view that's entirely outside of your application:
|
|
186
179
|
|
|
187
180
|
```ruby
|
|
188
181
|
render file: "/u/apps/warehouse_app/current/app/views/products/show"
|
|
189
182
|
```
|
|
190
183
|
|
|
191
|
-
The `:file` option takes an absolute file-system path. Of course, you need to have rights
|
|
184
|
+
The `:file` option takes an absolute file-system path. Of course, you need to have rights
|
|
185
|
+
to the view that you're using to render the content.
|
|
186
|
+
|
|
187
|
+
NOTE: Using the `:file` option in combination with users input can lead to security problems
|
|
188
|
+
since an attacker could use this action to access security sensitive files in your file system.
|
|
192
189
|
|
|
193
190
|
NOTE: By default, the file is rendered without using the current layout. If you want Rails to put the file into the current layout, you need to add the `layout: true` option.
|
|
194
191
|
|
|
195
|
-
TIP: If you're running Rails on Microsoft Windows, you should use the `:file` option to
|
|
192
|
+
TIP: If you're running Rails on Microsoft Windows, you should use the `:file` option to
|
|
193
|
+
render a file, because Windows filenames do not have the same format as Unix filenames.
|
|
196
194
|
|
|
197
195
|
#### Wrapping it up
|
|
198
196
|
|
|
@@ -237,15 +235,34 @@ render inline: "xml.p {'Horrid coding practice!'}", type: :builder
|
|
|
237
235
|
|
|
238
236
|
#### Rendering Text
|
|
239
237
|
|
|
240
|
-
You can send plain text - with no markup at all - back to the browser by using
|
|
238
|
+
You can send plain text - with no markup at all - back to the browser by using
|
|
239
|
+
the `:plain` option to `render`:
|
|
240
|
+
|
|
241
|
+
```ruby
|
|
242
|
+
render plain: "OK"
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
TIP: Rendering pure text is most useful when you're responding to Ajax or web
|
|
246
|
+
service requests that are expecting something other than proper HTML.
|
|
247
|
+
|
|
248
|
+
NOTE: By default, if you use the `:plain` option, the text is rendered without
|
|
249
|
+
using the current layout. If you want Rails to put the text into the current
|
|
250
|
+
layout, you need to add the `layout: true` option.
|
|
251
|
+
|
|
252
|
+
#### Rendering HTML
|
|
253
|
+
|
|
254
|
+
You can send a HTML string back to the browser by using the `:html` option to
|
|
255
|
+
`render`:
|
|
241
256
|
|
|
242
257
|
```ruby
|
|
243
|
-
render
|
|
258
|
+
render html: "<strong>Not Found</strong>".html_safe
|
|
244
259
|
```
|
|
245
260
|
|
|
246
|
-
TIP:
|
|
261
|
+
TIP: This is useful when you're rendering a small snippet of HTML code.
|
|
262
|
+
However, you might want to consider moving it to a template file if the markup
|
|
263
|
+
is complex.
|
|
247
264
|
|
|
248
|
-
NOTE:
|
|
265
|
+
NOTE: This option will escape HTML entities if the string is not html safe.
|
|
249
266
|
|
|
250
267
|
#### Rendering JSON
|
|
251
268
|
|
|
@@ -277,6 +294,22 @@ render js: "alert('Hello Rails');"
|
|
|
277
294
|
|
|
278
295
|
This will send the supplied string to the browser with a MIME type of `text/javascript`.
|
|
279
296
|
|
|
297
|
+
#### Rendering raw body
|
|
298
|
+
|
|
299
|
+
You can send a raw content back to the browser, without setting any content
|
|
300
|
+
type, by using the `:body` option to `render`:
|
|
301
|
+
|
|
302
|
+
```ruby
|
|
303
|
+
render body: "raw"
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
TIP: This option should be used only if you don't care about the content type of
|
|
307
|
+
the response. Using `:plain` or `:html` might be more appropriate in most of the
|
|
308
|
+
time.
|
|
309
|
+
|
|
310
|
+
NOTE: Unless overriden, your response returned from this render option will be
|
|
311
|
+
`text/html`, as that is the default content type of Action Dispatch response.
|
|
312
|
+
|
|
280
313
|
#### Options for `render`
|
|
281
314
|
|
|
282
315
|
Calls to the `render` method generally accept four options:
|
|
@@ -375,9 +408,9 @@ Rails understands both numeric status codes and the corresponding symbols shown
|
|
|
375
408
|
| | 423 | :locked |
|
|
376
409
|
| | 424 | :failed_dependency |
|
|
377
410
|
| | 426 | :upgrade_required |
|
|
378
|
-
| |
|
|
379
|
-
| |
|
|
380
|
-
| |
|
|
411
|
+
| | 428 | :precondition_required |
|
|
412
|
+
| | 429 | :too_many_requests |
|
|
413
|
+
| | 431 | :request_header_fields_too_large |
|
|
381
414
|
| **Server Error** | 500 | :internal_server_error |
|
|
382
415
|
| | 501 | :not_implemented |
|
|
383
416
|
| | 502 | :bad_gateway |
|
|
@@ -405,7 +438,7 @@ class ProductsController < ApplicationController
|
|
|
405
438
|
end
|
|
406
439
|
```
|
|
407
440
|
|
|
408
|
-
With this declaration, all of the views rendered by the
|
|
441
|
+
With this declaration, all of the views rendered by the `ProductsController` will use `app/views/layouts/inventory.html.erb` as their layout.
|
|
409
442
|
|
|
410
443
|
To assign a specific layout for the entire application, use a `layout` declaration in your `ApplicationController` class:
|
|
411
444
|
|
|
@@ -704,7 +737,7 @@ WARNING: The asset tag helpers do _not_ verify the existence of the assets at th
|
|
|
704
737
|
|
|
705
738
|
#### Linking to Feeds with the `auto_discovery_link_tag`
|
|
706
739
|
|
|
707
|
-
The `auto_discovery_link_tag` helper builds HTML that most browsers and
|
|
740
|
+
The `auto_discovery_link_tag` helper builds HTML that most browsers and feed readers can use to detect the presence of RSS or Atom feeds. It takes the type of the link (`:rss` or `:atom`), a hash of options that are passed through to url_for, and a hash of options for the tag:
|
|
708
741
|
|
|
709
742
|
```erb
|
|
710
743
|
<%= auto_discovery_link_tag(:rss, {action: "feed"},
|
|
@@ -1024,7 +1057,7 @@ You can also pass local variables into partials, making them even more powerful
|
|
|
1024
1057
|
```html+erb
|
|
1025
1058
|
<%= form_for(zone) do |f| %>
|
|
1026
1059
|
<p>
|
|
1027
|
-
<b>Zone name</b><br
|
|
1060
|
+
<b>Zone name</b><br>
|
|
1028
1061
|
<%= f.text_field :name %>
|
|
1029
1062
|
</p>
|
|
1030
1063
|
<p>
|
|
@@ -1120,11 +1153,11 @@ With this change, you can access an instance of the `@products` collection as th
|
|
|
1120
1153
|
You can also pass in arbitrary local variables to any partial you are rendering with the `locals: {}` option:
|
|
1121
1154
|
|
|
1122
1155
|
```erb
|
|
1123
|
-
<%= render partial: "
|
|
1156
|
+
<%= render partial: "product", collection: @products,
|
|
1124
1157
|
as: :item, locals: {title: "Products Page"} %>
|
|
1125
1158
|
```
|
|
1126
1159
|
|
|
1127
|
-
|
|
1160
|
+
In this case, the partial will have access to a local variable `title` with the value "Products Page".
|
|
1128
1161
|
|
|
1129
1162
|
TIP: Rails also makes a counter variable available within a partial called by the collection, named after the member of the collection followed by `_counter`. For example, if you're rendering `@products`, within the partial you can refer to `product_counter` to tell you how many times the partial has been rendered. This does not work in conjunction with the `as: :value` option.
|
|
1130
1163
|
|
|
@@ -39,7 +39,7 @@ Only the latest release series will receive bug fixes. When enough bugs are
|
|
|
39
39
|
fixed and its deemed worthy to release a new gem, this is the branch it happens
|
|
40
40
|
from.
|
|
41
41
|
|
|
42
|
-
**Currently included series:** `4.0.Z
|
|
42
|
+
**Currently included series:** `4.1.Z`, `4.0.Z`.
|
|
43
43
|
|
|
44
44
|
Security Issues
|
|
45
45
|
---------------
|
|
@@ -54,7 +54,7 @@ be built from 1.2.2, and then added to the end of 1-2-stable. This means that
|
|
|
54
54
|
security releases are easy to upgrade to if you're running the latest version
|
|
55
55
|
of Rails.
|
|
56
56
|
|
|
57
|
-
**Currently included series:** `4.
|
|
57
|
+
**Currently included series:** `4.1.Z`, `4.0.Z`.
|
|
58
58
|
|
|
59
59
|
Severe Security Issues
|
|
60
60
|
----------------------
|
|
@@ -63,7 +63,7 @@ For severe security issues we will provide new versions as above, and also the
|
|
|
63
63
|
last major release series will receive patches and new versions. The
|
|
64
64
|
classification of the security issue is judged by the core team.
|
|
65
65
|
|
|
66
|
-
**Currently included series:** `4.0.Z`, `3.2.Z
|
|
66
|
+
**Currently included series:** `4.1.Z`, `4.0.Z`, `3.2.Z`.
|
|
67
67
|
|
|
68
68
|
Unsupported Release Series
|
|
69
69
|
--------------------------
|
data/guides/source/migrations.md
CHANGED
|
@@ -120,7 +120,7 @@ Of course, calculating timestamps is no fun, so Active Record provides a
|
|
|
120
120
|
generator to handle making it for you:
|
|
121
121
|
|
|
122
122
|
```bash
|
|
123
|
-
$ rails generate migration AddPartNumberToProducts
|
|
123
|
+
$ bin/rails generate migration AddPartNumberToProducts
|
|
124
124
|
```
|
|
125
125
|
|
|
126
126
|
This will create an empty but appropriately named migration:
|
|
@@ -137,7 +137,7 @@ followed by a list of column names and types then a migration containing the
|
|
|
137
137
|
appropriate `add_column` and `remove_column` statements will be created.
|
|
138
138
|
|
|
139
139
|
```bash
|
|
140
|
-
$ rails generate migration AddPartNumberToProducts part_number:string
|
|
140
|
+
$ bin/rails generate migration AddPartNumberToProducts part_number:string
|
|
141
141
|
```
|
|
142
142
|
|
|
143
143
|
will generate
|
|
@@ -153,7 +153,7 @@ end
|
|
|
153
153
|
If you'd like to add an index on the new column, you can do that as well:
|
|
154
154
|
|
|
155
155
|
```bash
|
|
156
|
-
$ rails generate migration AddPartNumberToProducts part_number:string:index
|
|
156
|
+
$ bin/rails generate migration AddPartNumberToProducts part_number:string:index
|
|
157
157
|
```
|
|
158
158
|
|
|
159
159
|
will generate
|
|
@@ -171,7 +171,7 @@ end
|
|
|
171
171
|
Similarly, you can generate a migration to remove a column from the command line:
|
|
172
172
|
|
|
173
173
|
```bash
|
|
174
|
-
$ rails generate migration RemovePartNumberFromProducts part_number:string
|
|
174
|
+
$ bin/rails generate migration RemovePartNumberFromProducts part_number:string
|
|
175
175
|
```
|
|
176
176
|
|
|
177
177
|
generates
|
|
@@ -184,10 +184,10 @@ class RemovePartNumberFromProducts < ActiveRecord::Migration
|
|
|
184
184
|
end
|
|
185
185
|
```
|
|
186
186
|
|
|
187
|
-
You are not limited to one magically generated column. For example
|
|
187
|
+
You are not limited to one magically generated column. For example:
|
|
188
188
|
|
|
189
189
|
```bash
|
|
190
|
-
$ rails generate migration AddDetailsToProducts part_number:string price:decimal
|
|
190
|
+
$ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
|
|
191
191
|
```
|
|
192
192
|
|
|
193
193
|
generates
|
|
@@ -206,7 +206,7 @@ followed by a list of column names and types then a migration creating the table
|
|
|
206
206
|
XXX with the columns listed will be generated. For example:
|
|
207
207
|
|
|
208
208
|
```bash
|
|
209
|
-
$ rails generate migration CreateProducts name:string part_number:string
|
|
209
|
+
$ bin/rails generate migration CreateProducts name:string part_number:string
|
|
210
210
|
```
|
|
211
211
|
|
|
212
212
|
generates
|
|
@@ -227,10 +227,10 @@ or remove from it as you see fit by editing the
|
|
|
227
227
|
`db/migrate/YYYYMMDDHHMMSS_add_details_to_products.rb` file.
|
|
228
228
|
|
|
229
229
|
Also, the generator accepts column type as `references`(also available as
|
|
230
|
-
`belongs_to`). For instance
|
|
230
|
+
`belongs_to`). For instance:
|
|
231
231
|
|
|
232
232
|
```bash
|
|
233
|
-
$ rails generate migration AddUserRefToProducts user:references
|
|
233
|
+
$ bin/rails generate migration AddUserRefToProducts user:references
|
|
234
234
|
```
|
|
235
235
|
|
|
236
236
|
generates
|
|
@@ -248,7 +248,7 @@ This migration will create a `user_id` column and appropriate index.
|
|
|
248
248
|
There is also a generator which will produce join tables if `JoinTable` is part of the name:
|
|
249
249
|
|
|
250
250
|
```bash
|
|
251
|
-
rails g migration CreateJoinTableCustomerProduct customer product
|
|
251
|
+
$ bin/rails g migration CreateJoinTableCustomerProduct customer product
|
|
252
252
|
```
|
|
253
253
|
|
|
254
254
|
will produce the following migration:
|
|
@@ -269,10 +269,10 @@ end
|
|
|
269
269
|
The model and scaffold generators will create migrations appropriate for adding
|
|
270
270
|
a new model. This migration will already contain instructions for creating the
|
|
271
271
|
relevant table. If you tell Rails what columns you want, then statements for
|
|
272
|
-
adding these columns will also be created. For example, running
|
|
272
|
+
adding these columns will also be created. For example, running:
|
|
273
273
|
|
|
274
274
|
```bash
|
|
275
|
-
$ rails generate model Product name:string description:text
|
|
275
|
+
$ bin/rails generate model Product name:string description:text
|
|
276
276
|
```
|
|
277
277
|
|
|
278
278
|
will create a migration that looks like this
|
|
@@ -292,21 +292,15 @@ end
|
|
|
292
292
|
|
|
293
293
|
You can append as many column name/type pairs as you want.
|
|
294
294
|
|
|
295
|
-
###
|
|
295
|
+
### Passing Modifiers
|
|
296
296
|
|
|
297
|
-
|
|
298
|
-
|
|
297
|
+
Some commonly used [type modifiers](#column-modifiers) can be passed directly on
|
|
298
|
+
the command line. They are enclosed by curly braces and follow the field type:
|
|
299
299
|
|
|
300
|
-
|
|
301
|
-
* `precision` Defines the precision for the `decimal` fields
|
|
302
|
-
* `scale` Defines the scale for the `decimal` fields
|
|
303
|
-
* `polymorphic` Adds a `type` column for `belongs_to` associations
|
|
304
|
-
* `null` Allows or disallows `NULL` values in the column.
|
|
305
|
-
|
|
306
|
-
For instance, running
|
|
300
|
+
For instance, running:
|
|
307
301
|
|
|
308
302
|
```bash
|
|
309
|
-
$ rails generate migration AddDetailsToProducts price:decimal{5,2} supplier:references{polymorphic}
|
|
303
|
+
$ bin/rails generate migration AddDetailsToProducts 'price:decimal{5,2}' supplier:references{polymorphic}
|
|
310
304
|
```
|
|
311
305
|
|
|
312
306
|
will produce a migration that looks like this
|
|
@@ -320,6 +314,8 @@ class AddDetailsToProducts < ActiveRecord::Migration
|
|
|
320
314
|
end
|
|
321
315
|
```
|
|
322
316
|
|
|
317
|
+
TIP: Have a look at the generators help output for further details.
|
|
318
|
+
|
|
323
319
|
Writing a Migration
|
|
324
320
|
-------------------
|
|
325
321
|
|
|
@@ -345,7 +341,7 @@ By default, `create_table` will create a primary key called `id`. You can change
|
|
|
345
341
|
the name of the primary key with the `:primary_key` option (don't forget to
|
|
346
342
|
update the corresponding model) or, if you don't want a primary key at all, you
|
|
347
343
|
can pass the option `id: false`. If you need to pass database specific options
|
|
348
|
-
you can place an SQL fragment in the `:options` option. For example
|
|
344
|
+
you can place an SQL fragment in the `:options` option. For example:
|
|
349
345
|
|
|
350
346
|
```ruby
|
|
351
347
|
create_table :products, options: "ENGINE=BLACKHOLE" do |t|
|
|
@@ -359,7 +355,7 @@ will append `ENGINE=BLACKHOLE` to the SQL statement used to create the table
|
|
|
359
355
|
### Creating a Join Table
|
|
360
356
|
|
|
361
357
|
Migration method `create_join_table` creates a HABTM join table. A typical use
|
|
362
|
-
would be
|
|
358
|
+
would be:
|
|
363
359
|
|
|
364
360
|
```ruby
|
|
365
361
|
create_join_table :products, :categories
|
|
@@ -367,26 +363,24 @@ create_join_table :products, :categories
|
|
|
367
363
|
|
|
368
364
|
which creates a `categories_products` table with two columns called
|
|
369
365
|
`category_id` and `product_id`. These columns have the option `:null` set to
|
|
370
|
-
`false` by default.
|
|
371
|
-
|
|
372
|
-
You can pass the option `:table_name` when you want to customize the table
|
|
373
|
-
name. For example,
|
|
366
|
+
`false` by default. This can be overridden by specifying the `:column_options`
|
|
367
|
+
option.
|
|
374
368
|
|
|
375
369
|
```ruby
|
|
376
|
-
create_join_table :products, :categories,
|
|
370
|
+
create_join_table :products, :categories, column_options: {null: true}
|
|
377
371
|
```
|
|
378
372
|
|
|
379
|
-
will create
|
|
373
|
+
will create the `product_id` and `category_id` with the `:null` option as
|
|
374
|
+
`true`.
|
|
380
375
|
|
|
381
|
-
|
|
382
|
-
|
|
376
|
+
You can pass the option `:table_name` when you want to customize the table
|
|
377
|
+
name. For example:
|
|
383
378
|
|
|
384
379
|
```ruby
|
|
385
|
-
create_join_table :products, :categories,
|
|
380
|
+
create_join_table :products, :categories, table_name: :categorization
|
|
386
381
|
```
|
|
387
382
|
|
|
388
|
-
will create
|
|
389
|
-
`true`.
|
|
383
|
+
will create a `categorization` table.
|
|
390
384
|
|
|
391
385
|
`create_join_table` also accepts a block, which you can use to add indices
|
|
392
386
|
(which are not created by default) or additional columns:
|
|
@@ -402,7 +396,7 @@ end
|
|
|
402
396
|
|
|
403
397
|
A close cousin of `create_table` is `change_table`, used for changing existing
|
|
404
398
|
tables. It is used in a similar fashion to `create_table` but the object
|
|
405
|
-
yielded to the block knows more tricks. For example
|
|
399
|
+
yielded to the block knows more tricks. For example:
|
|
406
400
|
|
|
407
401
|
```ruby
|
|
408
402
|
change_table :products do |t|
|
|
@@ -416,6 +410,44 @@ end
|
|
|
416
410
|
removes the `description` and `name` columns, creates a `part_number` string
|
|
417
411
|
column and adds an index on it. Finally it renames the `upccode` column.
|
|
418
412
|
|
|
413
|
+
### Changing Columns
|
|
414
|
+
|
|
415
|
+
Like the `remove_column` and `add_column` Rails provides the `change_column`
|
|
416
|
+
migration method.
|
|
417
|
+
|
|
418
|
+
```ruby
|
|
419
|
+
change_column :products, :part_number, :text
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
This changes the column `part_number` on products table to be a `:text` field.
|
|
423
|
+
|
|
424
|
+
Besides `change_column`, the `change_column_null` and `change_column_default`
|
|
425
|
+
methods are used specifically to change the null and default values of a
|
|
426
|
+
column.
|
|
427
|
+
|
|
428
|
+
```ruby
|
|
429
|
+
change_column_null :products, :name, false
|
|
430
|
+
change_column_default :products, :approved, false
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
This sets `:name` field on products to a `NOT NULL` column and the default
|
|
434
|
+
value of the `:approved` field to false.
|
|
435
|
+
|
|
436
|
+
### Column Modifiers
|
|
437
|
+
|
|
438
|
+
Column modifiers can be applied when creating or changing a column:
|
|
439
|
+
|
|
440
|
+
* `limit` Sets the maximum size of the `string/text/binary/integer` fields.
|
|
441
|
+
* `precision` Defines the precision for the `decimal` fields, representing the total number of digits in the number.
|
|
442
|
+
* `scale` Defines the scale for the `decimal` fields, representing the number of digits after the decimal point.
|
|
443
|
+
* `polymorphic` Adds a `type` column for `belongs_to` associations.
|
|
444
|
+
* `null` Allows or disallows `NULL` values in the column.
|
|
445
|
+
* `default` Allows to set a default value on the column. NOTE: If using a dynamic value (such as date), the default will only be calculated the first time (e.g. on the date the migration is applied.)
|
|
446
|
+
* `index` Adds an index for the column.
|
|
447
|
+
|
|
448
|
+
Some adapters may support additional options; see the adapter specific API docs
|
|
449
|
+
for further information.
|
|
450
|
+
|
|
419
451
|
### When Helpers aren't Enough
|
|
420
452
|
|
|
421
453
|
If the helpers provided by Active Record aren't enough you can use the `execute`
|
|
@@ -449,7 +481,7 @@ definitions:
|
|
|
449
481
|
* `create_table`
|
|
450
482
|
* `create_join_table`
|
|
451
483
|
* `drop_table` (must supply a block)
|
|
452
|
-
* `drop_join_table`
|
|
484
|
+
* `drop_join_table` (must supply a block)
|
|
453
485
|
* `remove_timestamps`
|
|
454
486
|
* `rename_column`
|
|
455
487
|
* `rename_index`
|
|
@@ -466,7 +498,7 @@ or write the `up` and `down` methods instead of using the `change` method.
|
|
|
466
498
|
|
|
467
499
|
Complex migrations may require processing that Active Record doesn't know how
|
|
468
500
|
to reverse. You can use `reversible` to specify what to do when running a
|
|
469
|
-
migration what else to do when reverting it. For example
|
|
501
|
+
migration what else to do when reverting it. For example:
|
|
470
502
|
|
|
471
503
|
```ruby
|
|
472
504
|
class ExampleMigration < ActiveRecord::Migration
|
|
@@ -496,6 +528,7 @@ class ExampleMigration < ActiveRecord::Migration
|
|
|
496
528
|
add_column :users, :home_page_url, :string
|
|
497
529
|
rename_column :users, :email, :email_address
|
|
498
530
|
end
|
|
531
|
+
end
|
|
499
532
|
```
|
|
500
533
|
|
|
501
534
|
Using `reversible` will ensure that the instructions are executed in the
|
|
@@ -644,16 +677,16 @@ method for all the migrations that have not yet been run. If there are
|
|
|
644
677
|
no such migrations, it exits. It will run these migrations in order based
|
|
645
678
|
on the date of the migration.
|
|
646
679
|
|
|
647
|
-
Note that running the `db:migrate` also invokes the `db:schema:dump` task, which
|
|
680
|
+
Note that running the `db:migrate` task also invokes the `db:schema:dump` task, which
|
|
648
681
|
will update your `db/schema.rb` file to match the structure of your database.
|
|
649
682
|
|
|
650
683
|
If you specify a target version, Active Record will run the required migrations
|
|
651
684
|
(change, up, down) until it has reached the specified version. The version
|
|
652
685
|
is the numerical prefix on the migration's filename. For example, to migrate
|
|
653
|
-
to version 20080906120000 run
|
|
686
|
+
to version 20080906120000 run:
|
|
654
687
|
|
|
655
688
|
```bash
|
|
656
|
-
$ rake db:migrate VERSION=20080906120000
|
|
689
|
+
$ bin/rake db:migrate VERSION=20080906120000
|
|
657
690
|
```
|
|
658
691
|
|
|
659
692
|
If version 20080906120000 is greater than the current version (i.e., it is
|
|
@@ -667,10 +700,10 @@ down to, but not including, 20080906120000.
|
|
|
667
700
|
|
|
668
701
|
A common task is to rollback the last migration. For example, if you made a
|
|
669
702
|
mistake in it and wish to correct it. Rather than tracking down the version
|
|
670
|
-
number associated with the previous migration you can run
|
|
703
|
+
number associated with the previous migration you can run:
|
|
671
704
|
|
|
672
705
|
```bash
|
|
673
|
-
$ rake db:rollback
|
|
706
|
+
$ bin/rake db:rollback
|
|
674
707
|
```
|
|
675
708
|
|
|
676
709
|
This will rollback the latest migration, either by reverting the `change`
|
|
@@ -678,42 +711,47 @@ method or by running the `down` method. If you need to undo
|
|
|
678
711
|
several migrations you can provide a `STEP` parameter:
|
|
679
712
|
|
|
680
713
|
```bash
|
|
681
|
-
$ rake db:rollback STEP=3
|
|
714
|
+
$ bin/rake db:rollback STEP=3
|
|
682
715
|
```
|
|
683
716
|
|
|
684
717
|
will revert the last 3 migrations.
|
|
685
718
|
|
|
686
719
|
The `db:migrate:redo` task is a shortcut for doing a rollback and then migrating
|
|
687
720
|
back up again. As with the `db:rollback` task, you can use the `STEP` parameter
|
|
688
|
-
if you need to go more than one version back, for example
|
|
721
|
+
if you need to go more than one version back, for example:
|
|
689
722
|
|
|
690
723
|
```bash
|
|
691
|
-
$ rake db:migrate:redo STEP=3
|
|
724
|
+
$ bin/rake db:migrate:redo STEP=3
|
|
692
725
|
```
|
|
693
726
|
|
|
694
727
|
Neither of these Rake tasks do anything you could not do with `db:migrate`. They
|
|
695
728
|
are simply more convenient, since you do not need to explicitly specify the
|
|
696
729
|
version to migrate to.
|
|
697
730
|
|
|
731
|
+
### Setup the Database
|
|
732
|
+
|
|
733
|
+
The `rake db:setup` task will create the database, load the schema and initialize
|
|
734
|
+
it with the seed data.
|
|
735
|
+
|
|
698
736
|
### Resetting the Database
|
|
699
737
|
|
|
700
|
-
The `rake db:reset` task will drop the database
|
|
701
|
-
|
|
738
|
+
The `rake db:reset` task will drop the database and set it up again. This is
|
|
739
|
+
functionally equivalent to `rake db:drop db:setup`.
|
|
702
740
|
|
|
703
741
|
NOTE: This is not the same as running all the migrations. It will only use the
|
|
704
|
-
contents of the current schema.rb file. If a migration can't be rolled back,
|
|
705
|
-
|
|
706
|
-
|
|
742
|
+
contents of the current `schema.rb` file. If a migration can't be rolled back,
|
|
743
|
+
`rake db:reset` may not help you. To find out more about dumping the schema see
|
|
744
|
+
[Schema Dumping and You](#schema-dumping-and-you) section.
|
|
707
745
|
|
|
708
746
|
### Running Specific Migrations
|
|
709
747
|
|
|
710
748
|
If you need to run a specific migration up or down, the `db:migrate:up` and
|
|
711
749
|
`db:migrate:down` tasks will do that. Just specify the appropriate version and
|
|
712
750
|
the corresponding migration will have its `change`, `up` or `down` method
|
|
713
|
-
invoked, for example
|
|
751
|
+
invoked, for example:
|
|
714
752
|
|
|
715
753
|
```bash
|
|
716
|
-
$ rake db:migrate:up VERSION=20080906120000
|
|
754
|
+
$ bin/rake db:migrate:up VERSION=20080906120000
|
|
717
755
|
```
|
|
718
756
|
|
|
719
757
|
will run the 20080906120000 migration by running the `change` method (or the
|
|
@@ -729,7 +767,7 @@ To run migrations against another environment you can specify it using the
|
|
|
729
767
|
migrations against the `test` environment you could run:
|
|
730
768
|
|
|
731
769
|
```bash
|
|
732
|
-
$ rake db:migrate RAILS_ENV=test
|
|
770
|
+
$ bin/rake db:migrate RAILS_ENV=test
|
|
733
771
|
```
|
|
734
772
|
|
|
735
773
|
### Changing the Output of Running Migrations
|
|
@@ -752,7 +790,7 @@ Several methods are provided in migrations that allow you to control all this:
|
|
|
752
790
|
| say | Takes a message argument and outputs it as is. A second boolean argument can be passed to specify whether to indent or not.
|
|
753
791
|
| say_with_time | Outputs text along with how long it took to run its block. If the block returns an integer it assumes it is the number of rows affected.
|
|
754
792
|
|
|
755
|
-
For example, this migration
|
|
793
|
+
For example, this migration:
|
|
756
794
|
|
|
757
795
|
```ruby
|
|
758
796
|
class CreateProducts < ActiveRecord::Migration
|
|
@@ -853,7 +891,7 @@ She also adds a validation to the `Product` model for the new column:
|
|
|
853
891
|
# app/models/product.rb
|
|
854
892
|
|
|
855
893
|
class Product < ActiveRecord::Base
|
|
856
|
-
validates :flag,
|
|
894
|
+
validates :flag, inclusion: { in: [true, false] }
|
|
857
895
|
end
|
|
858
896
|
```
|
|
859
897
|
|
|
@@ -879,7 +917,8 @@ She also adds a validation to the `Product` model for the new column:
|
|
|
879
917
|
# app/models/product.rb
|
|
880
918
|
|
|
881
919
|
class Product < ActiveRecord::Base
|
|
882
|
-
validates :flag, :
|
|
920
|
+
validates :flag, inclusion: { in: [true, false] }
|
|
921
|
+
validates :fuzz, presence: true
|
|
883
922
|
end
|
|
884
923
|
```
|
|
885
924
|
|
|
@@ -1036,8 +1075,8 @@ this, then you should set the schema format to `:sql`.
|
|
|
1036
1075
|
Instead of using Active Record's schema dumper, the database's structure will
|
|
1037
1076
|
be dumped using a tool specific to the database (via the `db:structure:dump`
|
|
1038
1077
|
Rake task) into `db/structure.sql`. For example, for PostgreSQL, the `pg_dump`
|
|
1039
|
-
utility is used. For MySQL, this file will contain the output of
|
|
1040
|
-
TABLE` for the various tables.
|
|
1078
|
+
utility is used. For MySQL, this file will contain the output of
|
|
1079
|
+
`SHOW CREATE TABLE` for the various tables.
|
|
1041
1080
|
|
|
1042
1081
|
Loading these schemas is simply a question of executing the SQL statements they
|
|
1043
1082
|
contain. By definition, this will create a perfect copy of the database's
|
|
@@ -1066,8 +1105,8 @@ with foreign key constraints in the database.
|
|
|
1066
1105
|
|
|
1067
1106
|
Although Active Record does not provide any tools for working directly with
|
|
1068
1107
|
such features, the `execute` method can be used to execute arbitrary SQL. You
|
|
1069
|
-
|
|
1070
|
-
[foreigner](https://github.com/matthuhiggins/foreigner) which
|
|
1108
|
+
can also use a gem like
|
|
1109
|
+
[foreigner](https://github.com/matthuhiggins/foreigner) which adds foreign key
|
|
1071
1110
|
support to Active Record (including support for dumping foreign keys in
|
|
1072
1111
|
`db/schema.rb`).
|
|
1073
1112
|
|
|
@@ -9,7 +9,7 @@ After reading this guide, you will know:
|
|
|
9
9
|
|
|
10
10
|
--------------------------------------------------------------------------------
|
|
11
11
|
|
|
12
|
-
NOTE: This guide assumes the user knows how to use the [Rails form helpers](form_helpers.html) in general. Also, it
|
|
12
|
+
NOTE: This guide assumes the user knows how to use the [Rails form helpers](form_helpers.html) in general. Also, it's **not** an API reference. For a complete reference please visit [the Rails API documentation](http://api.rubyonrails.org/).
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
Model setup
|
|
@@ -56,7 +56,7 @@ end
|
|
|
56
56
|
|
|
57
57
|
### Custom model
|
|
58
58
|
|
|
59
|
-
As you might have inflected from this explanation, you _don
|
|
59
|
+
As you might have inflected from this explanation, you _don't_ necessarily need an ActiveRecord::Base model to use this functionality. The following examples are sufficient to enable the nested model form behavior:
|
|
60
60
|
|
|
61
61
|
#### Single associated object
|
|
62
62
|
|
|
@@ -177,7 +177,7 @@ When this form is posted the Rails parameter parser will construct a hash like t
|
|
|
177
177
|
}
|
|
178
178
|
```
|
|
179
179
|
|
|
180
|
-
That
|
|
180
|
+
That's it. The controller will simply pass this hash on to the model from the `create` action. The model will then handle building the `address` association for you and automatically save it when the parent (`person`) is saved.
|
|
181
181
|
|
|
182
182
|
#### Nested form for a collection of associated objects
|
|
183
183
|
|