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
|
@@ -28,7 +28,7 @@ For each controller there is an associated directory in the `app/views` director
|
|
|
28
28
|
Let's take a look at what Rails does by default when creating a new resource using the scaffold generator:
|
|
29
29
|
|
|
30
30
|
```bash
|
|
31
|
-
$ rails generate scaffold post
|
|
31
|
+
$ bin/rails generate scaffold post
|
|
32
32
|
[...]
|
|
33
33
|
invoke scaffold_controller
|
|
34
34
|
create app/controllers/posts_controller.rb
|
|
@@ -68,7 +68,7 @@ Consider the following loop for names:
|
|
|
68
68
|
```html+erb
|
|
69
69
|
<h1>Names of all the people</h1>
|
|
70
70
|
<% @people.each do |person| %>
|
|
71
|
-
Name: <%= person.name %><br
|
|
71
|
+
Name: <%= person.name %><br>
|
|
72
72
|
<% end %>
|
|
73
73
|
```
|
|
74
74
|
|
|
@@ -152,7 +152,7 @@ By default, Rails will compile each template to a method in order to render it.
|
|
|
152
152
|
|
|
153
153
|
### Partials
|
|
154
154
|
|
|
155
|
-
Partial templates
|
|
155
|
+
Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With partials, you can extract pieces of code from your templates to separate files and also reuse them throughout your templates.
|
|
156
156
|
|
|
157
157
|
#### Naming Partials
|
|
158
158
|
|
|
@@ -464,7 +464,7 @@ stylesheet_link_tag :monkey # =>
|
|
|
464
464
|
|
|
465
465
|
#### auto_discovery_link_tag
|
|
466
466
|
|
|
467
|
-
Returns a link tag that browsers and
|
|
467
|
+
Returns a link tag that browsers and feed readers can use to auto-detect an RSS or Atom feed.
|
|
468
468
|
|
|
469
469
|
```ruby
|
|
470
470
|
auto_discovery_link_tag(:rss, "http://www.example.com/feed.rss", {title: "RSS Feed"}) # =>
|
|
@@ -775,8 +775,8 @@ select_day(5)
|
|
|
775
775
|
Returns a select tag with options for each of the hours 0 through 23 with the current hour selected.
|
|
776
776
|
|
|
777
777
|
```ruby
|
|
778
|
-
# Generates a select field for
|
|
779
|
-
|
|
778
|
+
# Generates a select field for hours that defaults to the hours for the time provided
|
|
779
|
+
select_hour(Time.now + 6.hours)
|
|
780
780
|
```
|
|
781
781
|
|
|
782
782
|
#### select_minute
|
|
@@ -941,9 +941,9 @@ Creates a form and a scope around a specific model object that is used as a base
|
|
|
941
941
|
```html+erb
|
|
942
942
|
<%= form_for @post do |f| %>
|
|
943
943
|
<%= f.label :title, 'Title' %>:
|
|
944
|
-
<%= f.text_field :title %><br
|
|
944
|
+
<%= f.text_field :title %><br>
|
|
945
945
|
<%= f.label :body, 'Body' %>:
|
|
946
|
-
<%= f.text_area :body %><br
|
|
946
|
+
<%= f.text_area :body %><br>
|
|
947
947
|
<% end %>
|
|
948
948
|
```
|
|
949
949
|
|
|
@@ -1006,6 +1006,24 @@ text_field(:post, :title)
|
|
|
1006
1006
|
# => <input type="text" id="post_title" name="post[title]" value="#{@post.title}" />
|
|
1007
1007
|
```
|
|
1008
1008
|
|
|
1009
|
+
#### email_field
|
|
1010
|
+
|
|
1011
|
+
Returns an input tag of the "email" type tailored for accessing a specified attribute.
|
|
1012
|
+
|
|
1013
|
+
```ruby
|
|
1014
|
+
email_field(:user, :email)
|
|
1015
|
+
# => <input type="email" id="user_email" name="user[email]" value="#{@user.email}" />
|
|
1016
|
+
```
|
|
1017
|
+
|
|
1018
|
+
#### url_field
|
|
1019
|
+
|
|
1020
|
+
Returns an input tag of the "url" type tailored for accessing a specified attribute.
|
|
1021
|
+
|
|
1022
|
+
```ruby
|
|
1023
|
+
url_field(:user, :url)
|
|
1024
|
+
# => <input type="url" id="user_url" name="user[url]" value="#{@user.url}" />
|
|
1025
|
+
```
|
|
1026
|
+
|
|
1009
1027
|
### FormOptionsHelper
|
|
1010
1028
|
|
|
1011
1029
|
Provides a number of methods for turning different kinds of containers into a set of option tags.
|
|
@@ -1119,14 +1137,6 @@ If `@post.author_ids` is [1], this would return:
|
|
|
1119
1137
|
<input name="post[author_ids][]" type="hidden" value="" />
|
|
1120
1138
|
```
|
|
1121
1139
|
|
|
1122
|
-
#### country_options_for_select
|
|
1123
|
-
|
|
1124
|
-
Returns a string of option tags for pretty much any country in the world.
|
|
1125
|
-
|
|
1126
|
-
#### country_select
|
|
1127
|
-
|
|
1128
|
-
Return select and option tags for the given object and method, using country_options_for_select to generate the list of option tags.
|
|
1129
|
-
|
|
1130
1140
|
#### option_groups_from_collection_for_select
|
|
1131
1141
|
|
|
1132
1142
|
Returns a string of `option` tags, like `options_from_collection_for_select`, but groups them by `optgroup` tags based on the object relationships of the arguments.
|
|
@@ -1224,15 +1234,23 @@ Returns a string of option tags for pretty much any time zone in the world.
|
|
|
1224
1234
|
|
|
1225
1235
|
#### time_zone_select
|
|
1226
1236
|
|
|
1227
|
-
|
|
1237
|
+
Returns select and option tags for the given object and method, using `time_zone_options_for_select` to generate the list of option tags.
|
|
1228
1238
|
|
|
1229
1239
|
```ruby
|
|
1230
1240
|
time_zone_select( "user", "time_zone")
|
|
1231
1241
|
```
|
|
1232
1242
|
|
|
1243
|
+
#### date_field
|
|
1244
|
+
|
|
1245
|
+
Returns an input tag of the "date" type tailored for accessing a specified attribute.
|
|
1246
|
+
|
|
1247
|
+
```ruby
|
|
1248
|
+
date_field("user", "dob")
|
|
1249
|
+
```
|
|
1250
|
+
|
|
1233
1251
|
### FormTagHelper
|
|
1234
1252
|
|
|
1235
|
-
Provides a number of methods for creating form tags that
|
|
1253
|
+
Provides a number of methods for creating form tags that don't rely on an Active Record object assigned to the template like FormHelper does. Instead, you provide the names and values manually.
|
|
1236
1254
|
|
|
1237
1255
|
#### check_box_tag
|
|
1238
1256
|
|
|
@@ -1364,6 +1382,33 @@ text_field_tag 'name'
|
|
|
1364
1382
|
# => <input id="name" name="name" type="text" />
|
|
1365
1383
|
```
|
|
1366
1384
|
|
|
1385
|
+
#### email_field_tag
|
|
1386
|
+
|
|
1387
|
+
Creates a standard input field of email type.
|
|
1388
|
+
|
|
1389
|
+
```ruby
|
|
1390
|
+
email_field_tag 'email'
|
|
1391
|
+
# => <input id="email" name="email" type="email" />
|
|
1392
|
+
```
|
|
1393
|
+
|
|
1394
|
+
#### url_field_tag
|
|
1395
|
+
|
|
1396
|
+
Creates a standard input field of url type.
|
|
1397
|
+
|
|
1398
|
+
```ruby
|
|
1399
|
+
url_field_tag 'url'
|
|
1400
|
+
# => <input id="url" name="url" type="url" />
|
|
1401
|
+
```
|
|
1402
|
+
|
|
1403
|
+
#### date_field_tag
|
|
1404
|
+
|
|
1405
|
+
Creates a standard input field of date type.
|
|
1406
|
+
|
|
1407
|
+
```ruby
|
|
1408
|
+
date_field_tag "dob"
|
|
1409
|
+
# => <input id="dob" name="dob" type="date" />
|
|
1410
|
+
```
|
|
1411
|
+
|
|
1367
1412
|
### JavaScriptHelper
|
|
1368
1413
|
|
|
1369
1414
|
Provides functionality for working with JavaScript in your views.
|
|
@@ -1439,7 +1484,7 @@ number_to_human_size(1234567) # => 1.2 MB
|
|
|
1439
1484
|
Formats a number as a percentage string.
|
|
1440
1485
|
|
|
1441
1486
|
```ruby
|
|
1442
|
-
number_to_percentage(100, :
|
|
1487
|
+
number_to_percentage(100, precision: 0) # => 100%
|
|
1443
1488
|
```
|
|
1444
1489
|
|
|
1445
1490
|
#### number_to_phone
|
|
@@ -1467,12 +1512,78 @@ number_with_precision(111.2345) # => 111.235
|
|
|
1467
1512
|
number_with_precision(111.2345, 2) # => 111.23
|
|
1468
1513
|
```
|
|
1469
1514
|
|
|
1515
|
+
### SanitizeHelper
|
|
1516
|
+
|
|
1517
|
+
The SanitizeHelper module provides a set of methods for scrubbing text of undesired HTML elements.
|
|
1518
|
+
|
|
1519
|
+
#### sanitize
|
|
1520
|
+
|
|
1521
|
+
This sanitize helper will html encode all tags and strip all attributes that aren't specifically allowed.
|
|
1522
|
+
|
|
1523
|
+
```ruby
|
|
1524
|
+
sanitize @article.body
|
|
1525
|
+
```
|
|
1526
|
+
|
|
1527
|
+
If either the :attributes or :tags options are passed, only the mentioned tags and attributes are allowed and nothing else.
|
|
1528
|
+
|
|
1529
|
+
```ruby
|
|
1530
|
+
sanitize @article.body, tags: %w(table tr td), attributes: %w(id class style)
|
|
1531
|
+
```
|
|
1532
|
+
|
|
1533
|
+
To change defaults for multiple uses, for example adding table tags to the default:
|
|
1534
|
+
|
|
1535
|
+
```ruby
|
|
1536
|
+
class Application < Rails::Application
|
|
1537
|
+
config.action_view.sanitized_allowed_tags = 'table', 'tr', 'td'
|
|
1538
|
+
end
|
|
1539
|
+
```
|
|
1540
|
+
|
|
1541
|
+
#### sanitize_css(style)
|
|
1542
|
+
|
|
1543
|
+
Sanitizes a block of CSS code.
|
|
1544
|
+
|
|
1545
|
+
#### strip_links(html)
|
|
1546
|
+
Strips all link tags from text leaving just the link text.
|
|
1547
|
+
|
|
1548
|
+
```ruby
|
|
1549
|
+
strip_links("<a href="http://rubyonrails.org">Ruby on Rails</a>")
|
|
1550
|
+
# => Ruby on Rails
|
|
1551
|
+
```
|
|
1552
|
+
|
|
1553
|
+
```ruby
|
|
1554
|
+
strip_links("emails to <a href="mailto:me@email.com">me@email.com</a>.")
|
|
1555
|
+
# => emails to me@email.com.
|
|
1556
|
+
```
|
|
1557
|
+
|
|
1558
|
+
```ruby
|
|
1559
|
+
strip_links('Blog: <a href="http://myblog.com/">Visit</a>.')
|
|
1560
|
+
# => Blog: Visit.
|
|
1561
|
+
```
|
|
1562
|
+
|
|
1563
|
+
#### strip_tags(html)
|
|
1564
|
+
|
|
1565
|
+
Strips all HTML tags from the html, including comments.
|
|
1566
|
+
This uses the html-scanner tokenizer and so its HTML parsing ability is limited by that of html-scanner.
|
|
1567
|
+
|
|
1568
|
+
```ruby
|
|
1569
|
+
strip_tags("Strip <i>these</i> tags!")
|
|
1570
|
+
# => Strip these tags!
|
|
1571
|
+
```
|
|
1572
|
+
|
|
1573
|
+
```ruby
|
|
1574
|
+
strip_tags("<b>Bold</b> no more! <a href='more.html'>See more</a>")
|
|
1575
|
+
# => Bold no more! See more
|
|
1576
|
+
```
|
|
1577
|
+
|
|
1578
|
+
NB: The output may still contain unescaped '<', '>', '&' characters and confuse browsers.
|
|
1579
|
+
|
|
1580
|
+
|
|
1470
1581
|
Localized Views
|
|
1471
1582
|
---------------
|
|
1472
1583
|
|
|
1473
1584
|
Action View has the ability render different templates depending on the current locale.
|
|
1474
1585
|
|
|
1475
|
-
For example, suppose you have a
|
|
1586
|
+
For example, suppose you have a `PostsController` with a show action. By default, calling this action will render `app/views/posts/show.html.erb`. But if you set `I18n.locale = :de`, then `app/views/posts/show.de.html.erb` will be rendered instead. If the localized template isn't present, the undecorated version will be used. This means you're not required to provide localized views for all cases, but they will be preferred and used if available.
|
|
1476
1587
|
|
|
1477
1588
|
You can use the same technique to localize the rescue files in your public directory. For example, setting `I18n.locale = :de` and creating `public/500.de.html` and `public/404.de.html` would allow you to have localized rescue pages.
|
|
1478
1589
|
|
|
@@ -14,7 +14,7 @@ Active Model is a library containing various modules used in developing framewor
|
|
|
14
14
|
|
|
15
15
|
### AttributeMethods
|
|
16
16
|
|
|
17
|
-
The AttributeMethods module can add custom prefixes and suffixes on methods of a class. It is used by defining the prefixes and suffixes
|
|
17
|
+
The AttributeMethods module can add custom prefixes and suffixes on methods of a class. It is used by defining the prefixes and suffixes and which methods on the object will use them.
|
|
18
18
|
|
|
19
19
|
```ruby
|
|
20
20
|
class Person
|
|
@@ -45,7 +45,7 @@ person.age_highest? # false
|
|
|
45
45
|
|
|
46
46
|
### Callbacks
|
|
47
47
|
|
|
48
|
-
Callbacks gives Active Record style callbacks. This provides
|
|
48
|
+
Callbacks gives Active Record style callbacks. This provides an ability to define callbacks which run at appropriate times. After defining callbacks, you can wrap them with before, after and around custom methods.
|
|
49
49
|
|
|
50
50
|
```ruby
|
|
51
51
|
class Person
|
|
@@ -57,19 +57,19 @@ class Person
|
|
|
57
57
|
|
|
58
58
|
def update
|
|
59
59
|
run_callbacks(:update) do
|
|
60
|
-
# This
|
|
60
|
+
# This method is called when update is called on an object.
|
|
61
61
|
end
|
|
62
62
|
end
|
|
63
63
|
|
|
64
64
|
def reset_me
|
|
65
|
-
# This method
|
|
65
|
+
# This method is called when update is called on an object as a before_update callback is defined.
|
|
66
66
|
end
|
|
67
67
|
end
|
|
68
68
|
```
|
|
69
69
|
|
|
70
70
|
### Conversion
|
|
71
71
|
|
|
72
|
-
If a class defines `persisted?` and `id` methods then you can include `Conversion` module in that class and
|
|
72
|
+
If a class defines `persisted?` and `id` methods, then you can include the `Conversion` module in that class and call the Rails conversion methods on objects of that class.
|
|
73
73
|
|
|
74
74
|
```ruby
|
|
75
75
|
class Person
|
|
@@ -120,8 +120,8 @@ class Person
|
|
|
120
120
|
end
|
|
121
121
|
|
|
122
122
|
def save
|
|
123
|
-
@previously_changed = changes
|
|
124
123
|
# do save work...
|
|
124
|
+
changes_applied
|
|
125
125
|
end
|
|
126
126
|
end
|
|
127
127
|
```
|
|
@@ -48,10 +48,10 @@ overall database access code.
|
|
|
48
48
|
Active Record gives us several mechanisms, the most important being the ability
|
|
49
49
|
to:
|
|
50
50
|
|
|
51
|
-
* Represent models and their data
|
|
52
|
-
* Represent associations between these models
|
|
53
|
-
* Represent inheritance hierarchies through related models
|
|
54
|
-
* Validate models before they get persisted to the database
|
|
51
|
+
* Represent models and their data.
|
|
52
|
+
* Represent associations between these models.
|
|
53
|
+
* Represent inheritance hierarchies through related models.
|
|
54
|
+
* Validate models before they get persisted to the database.
|
|
55
55
|
* Perform database operations in an object-oriented fashion.
|
|
56
56
|
|
|
57
57
|
Convention over Configuration in Active Record
|
|
@@ -62,9 +62,9 @@ may be necessary to write a lot of configuration code. This is particularly true
|
|
|
62
62
|
for ORM frameworks in general. However, if you follow the conventions adopted by
|
|
63
63
|
Rails, you'll need to write very little configuration (in some case no
|
|
64
64
|
configuration at all) when creating Active Record models. The idea is that if
|
|
65
|
-
you configure your applications in the very same way most of the
|
|
66
|
-
should be the default way.
|
|
67
|
-
only in those cases where you can't follow the
|
|
65
|
+
you configure your applications in the very same way most of the time then this
|
|
66
|
+
should be the default way. Thus, explicit configuration would be needed
|
|
67
|
+
only in those cases where you can't follow the standard convention.
|
|
68
68
|
|
|
69
69
|
### Naming Conventions
|
|
70
70
|
|
|
@@ -78,15 +78,15 @@ of two or more words, the model class name should follow the Ruby conventions,
|
|
|
78
78
|
using the CamelCase form, while the table name must contain the words separated
|
|
79
79
|
by underscores. Examples:
|
|
80
80
|
|
|
81
|
-
* Database Table - Plural with underscores separating words (e.g., `book_clubs`)
|
|
81
|
+
* Database Table - Plural with underscores separating words (e.g., `book_clubs`).
|
|
82
82
|
* Model Class - Singular with the first letter of each word capitalized (e.g.,
|
|
83
|
-
`BookClub`)
|
|
83
|
+
`BookClub`).
|
|
84
84
|
|
|
85
85
|
| Model / Class | Table / Schema |
|
|
86
86
|
| ------------- | -------------- |
|
|
87
87
|
| `Post` | `posts` |
|
|
88
88
|
| `LineItem` | `line_items` |
|
|
89
|
-
| `Deer` | `
|
|
89
|
+
| `Deer` | `deers` |
|
|
90
90
|
| `Mouse` | `mice` |
|
|
91
91
|
| `Person` | `people` |
|
|
92
92
|
|
|
@@ -101,7 +101,7 @@ depending on the purpose of these columns.
|
|
|
101
101
|
fields that Active Record will look for when you create associations between
|
|
102
102
|
your models.
|
|
103
103
|
* **Primary keys** - By default, Active Record will use an integer column named
|
|
104
|
-
`id` as the table's primary key. When using [
|
|
104
|
+
`id` as the table's primary key. When using [Active Record
|
|
105
105
|
Migrations](migrations.html) to create your tables, this column will be
|
|
106
106
|
automatically created.
|
|
107
107
|
|
|
@@ -181,7 +181,7 @@ definition:
|
|
|
181
181
|
|
|
182
182
|
```ruby
|
|
183
183
|
class FunnyJoke < ActiveSupport::TestCase
|
|
184
|
-
set_fixture_class funny_jokes:
|
|
184
|
+
set_fixture_class funny_jokes: Joke
|
|
185
185
|
fixtures :funny_jokes
|
|
186
186
|
...
|
|
187
187
|
end
|
|
@@ -368,6 +368,6 @@ Rails keeps track of which files have been committed to the database and
|
|
|
368
368
|
provides rollback features. To actually create the table, you'd run `rake db:migrate`
|
|
369
369
|
and to roll it back, `rake db:rollback`.
|
|
370
370
|
|
|
371
|
-
Note that the above code is database-agnostic: it will run in MySQL,
|
|
372
|
-
Oracle and others. You can learn more about migrations in the
|
|
373
|
-
Migrations guide](migrations.html)
|
|
371
|
+
Note that the above code is database-agnostic: it will run in MySQL,
|
|
372
|
+
PostgreSQL, Oracle and others. You can learn more about migrations in the
|
|
373
|
+
[Active Record Migrations guide](migrations.html).
|
|
@@ -35,11 +35,11 @@ class User < ActiveRecord::Base
|
|
|
35
35
|
before_validation :ensure_login_has_a_value
|
|
36
36
|
|
|
37
37
|
protected
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
def ensure_login_has_a_value
|
|
39
|
+
if login.nil?
|
|
40
|
+
self.login = email unless email.blank?
|
|
41
|
+
end
|
|
41
42
|
end
|
|
42
|
-
end
|
|
43
43
|
end
|
|
44
44
|
```
|
|
45
45
|
|
|
@@ -49,8 +49,8 @@ The macro-style class methods can also receive a block. Consider using this styl
|
|
|
49
49
|
class User < ActiveRecord::Base
|
|
50
50
|
validates :login, :email, presence: true
|
|
51
51
|
|
|
52
|
-
before_create do
|
|
53
|
-
|
|
52
|
+
before_create do
|
|
53
|
+
self.name = login.capitalize if name.blank?
|
|
54
54
|
end
|
|
55
55
|
end
|
|
56
56
|
```
|
|
@@ -65,13 +65,13 @@ class User < ActiveRecord::Base
|
|
|
65
65
|
after_validation :set_location, on: [ :create, :update ]
|
|
66
66
|
|
|
67
67
|
protected
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
def normalize_name
|
|
69
|
+
self.name = self.name.downcase.titleize
|
|
70
|
+
end
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
72
|
+
def set_location
|
|
73
|
+
self.location = LocationService.query(self)
|
|
74
|
+
end
|
|
75
75
|
end
|
|
76
76
|
```
|
|
77
77
|
|
|
@@ -229,7 +229,7 @@ NOTE: The `find_by_*` and `find_by_*!` methods are dynamic finders generated aut
|
|
|
229
229
|
Skipping Callbacks
|
|
230
230
|
------------------
|
|
231
231
|
|
|
232
|
-
Just as with validations, it is also possible to skip callbacks
|
|
232
|
+
Just as with validations, it is also possible to skip callbacks by using the following methods:
|
|
233
233
|
|
|
234
234
|
* `decrement`
|
|
235
235
|
* `decrement_counter`
|
|
@@ -244,6 +244,8 @@ Just as with validations, it is also possible to skip callbacks. These methods s
|
|
|
244
244
|
* `update_all`
|
|
245
245
|
* `update_counters`
|
|
246
246
|
|
|
247
|
+
These methods should be used with caution, however, because important business rules and application logic may be kept in callbacks. Bypassing them without understanding the potential implications may lead to invalid data.
|
|
248
|
+
|
|
247
249
|
Halting Execution
|
|
248
250
|
-----------------
|
|
249
251
|
|
|
@@ -251,7 +253,7 @@ As you start registering new callbacks for your models, they will be queued for
|
|
|
251
253
|
|
|
252
254
|
The whole callback chain is wrapped in a transaction. If any _before_ callback method returns exactly `false` or raises an exception, the execution chain gets halted and a ROLLBACK is issued; _after_ callbacks can only accomplish that by raising an exception.
|
|
253
255
|
|
|
254
|
-
WARNING.
|
|
256
|
+
WARNING. Any exception that is not `ActiveRecord::Rollback` will be re-raised by Rails after the callback chain is halted. Raising an exception other than `ActiveRecord::Rollback` may break code that does not expect methods like `save` and `update_attributes` (which normally try to return `true` or `false`) to raise an exception.
|
|
255
257
|
|
|
256
258
|
Relational Callbacks
|
|
257
259
|
--------------------
|
|
@@ -392,7 +394,7 @@ By using the `after_commit` callback we can account for this case.
|
|
|
392
394
|
|
|
393
395
|
```ruby
|
|
394
396
|
class PictureFile < ActiveRecord::Base
|
|
395
|
-
after_commit :delete_picture_file_from_disk, :
|
|
397
|
+
after_commit :delete_picture_file_from_disk, on: [:destroy]
|
|
396
398
|
|
|
397
399
|
def delete_picture_file_from_disk
|
|
398
400
|
if File.exist?(filepath)
|
|
@@ -405,4 +407,4 @@ end
|
|
|
405
407
|
NOTE: the `:on` option specifies when a callback will be fired. If you
|
|
406
408
|
don't supply the `:on` option the callback will fire for every action.
|
|
407
409
|
|
|
408
|
-
The `after_commit` and `after_rollback` callbacks are guaranteed to be called for all models created, updated, or destroyed within a transaction block. If any exceptions are raised within one of these callbacks, they will be ignored so that they don't interfere with the other callbacks. As such, if your callback code could raise an exception, you'll need to rescue it and handle it appropriately within the callback.
|
|
410
|
+
WARNING. The `after_commit` and `after_rollback` callbacks are guaranteed to be called for all models created, updated, or destroyed within a transaction block. If any exceptions are raised within one of these callbacks, they will be ignored so that they don't interfere with the other callbacks. As such, if your callback code could raise an exception, you'll need to rescue it and handle it appropriately within the callback.
|