rails 4.2.0.beta3 → 4.2.0.beta4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -7
- data/guides/output/2_2_release_notes.html +724 -0
- data/guides/output/2_3_release_notes.html +870 -0
- data/guides/output/3_0_release_notes.html +773 -0
- data/guides/output/3_1_release_notes.html +740 -0
- data/guides/output/3_2_release_notes.html +797 -0
- data/guides/output/4_0_release_notes.html +523 -0
- data/guides/output/4_1_release_notes.html +806 -0
- data/guides/output/4_2_release_notes.html +728 -0
- data/guides/output/Gemfile +6 -0
- data/guides/output/_license.html +226 -0
- data/guides/output/_welcome.html +233 -0
- data/guides/output/action_controller_overview.html +1335 -0
- data/guides/output/action_mailer_basics.html +928 -0
- data/guides/output/action_view_overview.html +1509 -0
- data/guides/output/active_job_basics.html +546 -0
- data/guides/output/active_model_basics.html +438 -0
- data/guides/output/active_record_basics.html +594 -0
- data/guides/output/active_record_callbacks.html +592 -0
- data/guides/output/active_record_migrations.html +1123 -0
- data/guides/output/active_record_postgresql.html +675 -0
- data/guides/output/active_record_querying.html +1796 -0
- data/guides/output/active_record_validations.html +1301 -0
- data/guides/output/active_support_core_extensions.html +3450 -0
- data/guides/output/active_support_instrumentation.html +1121 -0
- data/guides/output/api_documentation_guidelines.html +498 -0
- data/guides/output/asset_pipeline.html +1167 -0
- data/guides/output/association_basics.html +2086 -0
- data/guides/output/caching_with_rails.html +553 -0
- data/guides/output/command_line.html +791 -0
- data/guides/output/configuring.html +1055 -0
- data/guides/output/contributing_to_ruby_on_rails.html +657 -0
- data/guides/output/credits.html +284 -0
- data/guides/output/debugging_rails_applications.html +1014 -0
- data/guides/output/development_dependencies_install.html +478 -0
- data/guides/output/engines.html +1438 -0
- data/guides/output/form_helpers.html +1074 -0
- data/guides/output/generators.html +838 -0
- data/guides/output/getting_started.html +2092 -0
- data/guides/output/i18n.html +1198 -0
- data/guides/output/images/akshaysurve.jpg +0 -0
- data/guides/output/images/belongs_to.png +0 -0
- data/guides/output/images/book_icon.gif +0 -0
- data/guides/output/images/bullet.gif +0 -0
- data/guides/output/images/chapters_icon.gif +0 -0
- data/guides/output/images/check_bullet.gif +0 -0
- data/guides/output/images/credits_pic_blank.gif +0 -0
- data/guides/output/images/csrf.png +0 -0
- data/guides/output/images/edge_badge.png +0 -0
- data/guides/output/images/favicon.ico +0 -0
- data/guides/output/images/feature_tile.gif +0 -0
- data/guides/output/images/footer_tile.gif +0 -0
- data/guides/output/images/fxn.png +0 -0
- data/guides/output/images/getting_started/article_with_comments.png +0 -0
- data/guides/output/images/getting_started/challenge.png +0 -0
- data/guides/output/images/getting_started/confirm_dialog.png +0 -0
- data/guides/output/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
- data/guides/output/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
- data/guides/output/images/getting_started/form_with_errors.png +0 -0
- data/guides/output/images/getting_started/index_action_with_edit_link.png +0 -0
- data/guides/output/images/getting_started/new_article.png +0 -0
- data/guides/output/images/getting_started/new_post.png +0 -0
- data/guides/output/images/getting_started/post_with_comments.png +0 -0
- data/guides/output/images/getting_started/rails_welcome.png +0 -0
- data/guides/output/images/getting_started/routing_error_no_controller.png +0 -0
- data/guides/output/images/getting_started/routing_error_no_route_matches.png +0 -0
- data/guides/output/images/getting_started/show_action_for_articles.png +0 -0
- data/guides/output/images/getting_started/show_action_for_posts.png +0 -0
- data/guides/output/images/getting_started/template_is_missing_articles_new.png +0 -0
- data/guides/output/images/getting_started/template_is_missing_posts_new.png +0 -0
- data/guides/output/images/getting_started/undefined_method_post_path.png +0 -0
- data/guides/output/images/getting_started/unknown_action_create_for_articles.png +0 -0
- data/guides/output/images/getting_started/unknown_action_create_for_posts.png +0 -0
- data/guides/output/images/getting_started/unknown_action_new_for_articles.png +0 -0
- data/guides/output/images/getting_started/unknown_action_new_for_posts.png +0 -0
- data/guides/output/images/grey_bullet.gif +0 -0
- data/guides/output/images/habtm.png +0 -0
- data/guides/output/images/has_many.png +0 -0
- data/guides/output/images/has_many_through.png +0 -0
- data/guides/output/images/has_one.png +0 -0
- data/guides/output/images/has_one_through.png +0 -0
- data/guides/output/images/header_backdrop.png +0 -0
- data/guides/output/images/header_tile.gif +0 -0
- data/guides/output/images/i18n/demo_html_safe.png +0 -0
- data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
- data/guides/output/images/i18n/demo_translated_en.png +0 -0
- data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
- data/guides/output/images/i18n/demo_translation_missing.png +0 -0
- data/guides/output/images/i18n/demo_untranslated.png +0 -0
- data/guides/output/images/icons/README +5 -0
- data/guides/output/images/icons/callouts/1.png +0 -0
- data/guides/output/images/icons/callouts/10.png +0 -0
- data/guides/output/images/icons/callouts/11.png +0 -0
- data/guides/output/images/icons/callouts/12.png +0 -0
- data/guides/output/images/icons/callouts/13.png +0 -0
- data/guides/output/images/icons/callouts/14.png +0 -0
- data/guides/output/images/icons/callouts/15.png +0 -0
- data/guides/output/images/icons/callouts/2.png +0 -0
- data/guides/output/images/icons/callouts/3.png +0 -0
- data/guides/output/images/icons/callouts/4.png +0 -0
- data/guides/output/images/icons/callouts/5.png +0 -0
- data/guides/output/images/icons/callouts/6.png +0 -0
- data/guides/output/images/icons/callouts/7.png +0 -0
- data/guides/output/images/icons/callouts/8.png +0 -0
- data/guides/output/images/icons/callouts/9.png +0 -0
- data/guides/output/images/icons/caution.png +0 -0
- data/guides/output/images/icons/example.png +0 -0
- data/guides/output/images/icons/home.png +0 -0
- data/guides/output/images/icons/important.png +0 -0
- data/guides/output/images/icons/next.png +0 -0
- data/guides/output/images/icons/note.png +0 -0
- data/guides/output/images/icons/prev.png +0 -0
- data/guides/output/images/icons/tip.png +0 -0
- data/guides/output/images/icons/up.png +0 -0
- data/guides/output/images/icons/warning.png +0 -0
- data/guides/output/images/nav_arrow.gif +0 -0
- data/guides/output/images/oscardelben.jpg +0 -0
- data/guides/output/images/polymorphic.png +0 -0
- data/guides/output/images/radar.png +0 -0
- data/guides/output/images/rails4_features.png +0 -0
- data/guides/output/images/rails_guides_kindle_cover.jpg +0 -0
- data/guides/output/images/rails_guides_logo.gif +0 -0
- data/guides/output/images/rails_logo_remix.gif +0 -0
- data/guides/output/images/session_fixation.png +0 -0
- data/guides/output/images/tab_grey.gif +0 -0
- data/guides/output/images/tab_info.gif +0 -0
- data/guides/output/images/tab_note.gif +0 -0
- data/guides/output/images/tab_red.gif +0 -0
- data/guides/output/images/tab_yellow.gif +0 -0
- data/guides/output/images/tab_yellow.png +0 -0
- data/guides/output/images/vijaydev.jpg +0 -0
- data/guides/output/index.html +354 -0
- data/guides/output/initialization.html +876 -0
- data/guides/output/javascripts/guides.js +59 -0
- data/guides/output/javascripts/jquery.min.js +4 -0
- data/guides/output/javascripts/responsive-tables.js +43 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
- data/guides/output/javascripts/syntaxhighlighter/shCore.js +17 -0
- data/guides/output/layout.html +448 -0
- data/guides/output/layouts_and_rendering.html +1541 -0
- data/guides/output/maintenance_policy.html +257 -0
- data/guides/output/migrations.html +1360 -0
- data/guides/output/nested_model_forms.html +412 -0
- data/guides/output/plugins.html +644 -0
- data/guides/output/rails_application_templates.html +450 -0
- data/guides/output/rails_on_rack.html +547 -0
- data/guides/output/routing.html +1631 -0
- data/guides/output/ruby_on_rails_guides_guidelines.html +329 -0
- data/guides/output/security.html +935 -0
- data/guides/output/stylesheets/fixes.css +16 -0
- data/guides/output/stylesheets/kindle.css +11 -0
- data/guides/output/stylesheets/main.css +713 -0
- data/guides/output/stylesheets/print.css +52 -0
- data/guides/output/stylesheets/reset.css +43 -0
- data/guides/output/stylesheets/responsive-tables.css +50 -0
- data/guides/output/stylesheets/style.css +13 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCore.css +226 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
- data/guides/output/testing.html +1350 -0
- data/guides/output/upgrading_ruby_on_rails.html +1135 -0
- data/guides/output/working_with_javascript_in_rails.html +587 -0
- data/guides/source/4_0_release_notes.md +2 -2
- data/guides/source/4_2_release_notes.md +9 -2
- data/guides/source/action_controller_overview.md +3 -1
- data/guides/source/action_mailer_basics.md +1 -2
- data/guides/source/active_job_basics.md +25 -2
- data/guides/source/active_model_basics.md +350 -19
- data/guides/source/active_record_basics.md +2 -2
- data/guides/source/active_record_migrations.md +1 -1
- data/guides/source/active_record_validations.md +12 -4
- data/guides/source/active_support_core_extensions.md +7 -9
- data/guides/source/active_support_instrumentation.md +2 -0
- data/guides/source/association_basics.md +7 -7
- data/guides/source/configuring.md +24 -0
- data/guides/source/contributing_to_ruby_on_rails.md +1 -1
- data/guides/source/credits.html.erb +1 -1
- data/guides/source/documents.yaml +5 -0
- data/guides/source/engines.md +2 -2
- data/guides/source/form_helpers.md +6 -0
- data/guides/source/i18n.md +6 -9
- data/guides/source/layouts_and_rendering.md +1 -1
- data/guides/source/plugins.md +2 -2
- data/guides/source/rails_on_rack.md +4 -0
- data/guides/source/testing.md +1 -0
- data/guides/source/upgrading_ruby_on_rails.md +5 -5
- metadata +227 -27
@@ -0,0 +1,592 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
|
3
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
4
|
+
<head>
|
5
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
7
|
+
|
8
|
+
<title>Active Record Callbacks — Ruby on Rails Guides</title>
|
9
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
|
10
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
|
11
|
+
|
12
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
|
13
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
|
14
|
+
|
15
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
|
16
|
+
|
17
|
+
<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
|
18
|
+
</head>
|
19
|
+
<body class="guide">
|
20
|
+
<div id="topNav">
|
21
|
+
<div class="wrapper">
|
22
|
+
<strong class="more-info-label">More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
|
23
|
+
<span class="red-button more-info-button">
|
24
|
+
More Ruby on Rails
|
25
|
+
</span>
|
26
|
+
<ul class="more-info-links s-hidden">
|
27
|
+
<li class="more-info"><a href="http://rubyonrails.org/">Overview</a></li>
|
28
|
+
<li class="more-info"><a href="http://rubyonrails.org/download">Download</a></li>
|
29
|
+
<li class="more-info"><a href="http://rubyonrails.org/deploy">Deploy</a></li>
|
30
|
+
<li class="more-info"><a href="https://github.com/rails/rails">Code</a></li>
|
31
|
+
<li class="more-info"><a href="http://rubyonrails.org/screencasts">Screencasts</a></li>
|
32
|
+
<li class="more-info"><a href="http://rubyonrails.org/documentation">Documentation</a></li>
|
33
|
+
<li class="more-info"><a href="http://rubyonrails.org/community">Community</a></li>
|
34
|
+
<li class="more-info"><a href="http://weblog.rubyonrails.org/">Blog</a></li>
|
35
|
+
</ul>
|
36
|
+
</div>
|
37
|
+
</div>
|
38
|
+
<div id="header">
|
39
|
+
<div class="wrapper clearfix">
|
40
|
+
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
|
41
|
+
<ul class="nav">
|
42
|
+
<li><a class="nav-item" href="index.html">Home</a></li>
|
43
|
+
<li class="guides-index guides-index-large">
|
44
|
+
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Guides Index</a>
|
45
|
+
<div id="guides" class="clearfix" style="display: none;">
|
46
|
+
<hr />
|
47
|
+
<dl class="L">
|
48
|
+
<dt>Start Here</dt>
|
49
|
+
<dd><a href="getting_started.html">Getting Started with Rails</a></dd>
|
50
|
+
<dt>Models</dt>
|
51
|
+
<dd><a href="active_record_basics.html">Active Record Basics</a></dd>
|
52
|
+
<dd><a href="active_record_migrations.html">Active Record Migrations</a></dd>
|
53
|
+
<dd><a href="active_record_validations.html">Active Record Validations</a></dd>
|
54
|
+
<dd><a href="active_record_callbacks.html">Active Record Callbacks</a></dd>
|
55
|
+
<dd><a href="association_basics.html">Active Record Associations</a></dd>
|
56
|
+
<dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
|
57
|
+
<dt>Views</dt>
|
58
|
+
<dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
|
59
|
+
<dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
|
60
|
+
<dt>Controllers</dt>
|
61
|
+
<dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
|
62
|
+
<dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
|
63
|
+
</dl>
|
64
|
+
<dl class="R">
|
65
|
+
<dt>Digging Deeper</dt>
|
66
|
+
<dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
|
67
|
+
<dd><a href="i18n.html">Rails Internationalization API</a></dd>
|
68
|
+
<dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
|
69
|
+
<dd><a href="active_job_basics.html">Active Job Basics</a></dd>
|
70
|
+
<dd><a href="security.html">Securing Rails Applications</a></dd>
|
71
|
+
<dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
|
72
|
+
<dd><a href="configuring.html">Configuring Rails Applications</a></dd>
|
73
|
+
<dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
|
74
|
+
<dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
|
75
|
+
<dd><a href="working_with_javascript_in_rails.html">Working with JavaScript in Rails</a></dd>
|
76
|
+
<dt>Extending Rails</dt>
|
77
|
+
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
|
78
|
+
<dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
|
79
|
+
<dt>Contributing to Ruby on Rails</dt>
|
80
|
+
<dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
|
81
|
+
<dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
|
82
|
+
<dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
|
83
|
+
<dt>Maintenance Policy</dt>
|
84
|
+
<dd><a href="maintenance_policy.html">Maintenance Policy</a></dd>
|
85
|
+
<dt>Release Notes</dt>
|
86
|
+
<dd><a href="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</a></dd>
|
87
|
+
<dd><a href="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</a></dd>
|
88
|
+
<dd><a href="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</a></dd>
|
89
|
+
<dd><a href="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</a></dd>
|
90
|
+
<dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
|
91
|
+
<dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
|
92
|
+
<dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
|
93
|
+
<dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
|
94
|
+
</dl>
|
95
|
+
</div>
|
96
|
+
</li>
|
97
|
+
<li><a class="nav-item" href="contributing_to_ruby_on_rails.html">Contribute</a></li>
|
98
|
+
<li><a class="nav-item" href="credits.html">Credits</a></li>
|
99
|
+
<li class="guides-index guides-index-small">
|
100
|
+
<select class="guides-index-item nav-item">
|
101
|
+
<option value="index.html">Guides Index</option>
|
102
|
+
<optgroup label="Start Here">
|
103
|
+
<option value="getting_started.html">Getting Started with Rails</option>
|
104
|
+
</optgroup>
|
105
|
+
<optgroup label="Models">
|
106
|
+
<option value="active_record_basics.html">Active Record Basics</option>
|
107
|
+
<option value="active_record_migrations.html">Active Record Migrations</option>
|
108
|
+
<option value="active_record_validations.html">Active Record Validations</option>
|
109
|
+
<option value="active_record_callbacks.html">Active Record Callbacks</option>
|
110
|
+
<option value="association_basics.html">Active Record Associations</option>
|
111
|
+
<option value="active_record_querying.html">Active Record Query Interface</option>
|
112
|
+
</optgroup>
|
113
|
+
<optgroup label="Views">
|
114
|
+
<option value="layouts_and_rendering.html">Layouts and Rendering in Rails</option>
|
115
|
+
<option value="form_helpers.html">Action View Form Helpers</option>
|
116
|
+
</optgroup>
|
117
|
+
<optgroup label="Controllers">
|
118
|
+
<option value="action_controller_overview.html">Action Controller Overview</option>
|
119
|
+
<option value="routing.html">Rails Routing from the Outside In</option>
|
120
|
+
</optgroup>
|
121
|
+
<optgroup label="Digging Deeper">
|
122
|
+
<option value="active_support_core_extensions.html">Active Support Core Extensions</option>
|
123
|
+
<option value="i18n.html">Rails Internationalization API</option>
|
124
|
+
<option value="action_mailer_basics.html">Action Mailer Basics</option>
|
125
|
+
<option value="active_job_basics.html">Active Job Basics</option>
|
126
|
+
<option value="security.html">Securing Rails Applications</option>
|
127
|
+
<option value="debugging_rails_applications.html">Debugging Rails Applications</option>
|
128
|
+
<option value="configuring.html">Configuring Rails Applications</option>
|
129
|
+
<option value="command_line.html">Rails Command Line Tools and Rake Tasks</option>
|
130
|
+
<option value="asset_pipeline.html">Asset Pipeline</option>
|
131
|
+
<option value="working_with_javascript_in_rails.html">Working with JavaScript in Rails</option>
|
132
|
+
</optgroup>
|
133
|
+
<optgroup label="Extending Rails">
|
134
|
+
<option value="rails_on_rack.html">Rails on Rack</option>
|
135
|
+
<option value="generators.html">Creating and Customizing Rails Generators</option>
|
136
|
+
</optgroup>
|
137
|
+
<optgroup label="Contributing to Ruby on Rails">
|
138
|
+
<option value="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</option>
|
139
|
+
<option value="api_documentation_guidelines.html">API Documentation Guidelines</option>
|
140
|
+
<option value="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</option>
|
141
|
+
</optgroup>
|
142
|
+
<optgroup label="Maintenance Policy">
|
143
|
+
<option value="maintenance_policy.html">Maintenance Policy</option>
|
144
|
+
</optgroup>
|
145
|
+
<optgroup label="Release Notes">
|
146
|
+
<option value="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</option>
|
147
|
+
<option value="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</option>
|
148
|
+
<option value="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</option>
|
149
|
+
<option value="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</option>
|
150
|
+
<option value="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</option>
|
151
|
+
<option value="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</option>
|
152
|
+
<option value="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</option>
|
153
|
+
<option value="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</option>
|
154
|
+
</optgroup>
|
155
|
+
</select>
|
156
|
+
</li>
|
157
|
+
</ul>
|
158
|
+
</div>
|
159
|
+
</div>
|
160
|
+
<hr class="hide" />
|
161
|
+
|
162
|
+
<div id="feature">
|
163
|
+
<div class="wrapper">
|
164
|
+
<h2>Active Record Callbacks</h2><p>This guide teaches you how to hook into the life cycle of your Active Record
|
165
|
+
objects.</p><p>After reading this guide, you will know:</p>
|
166
|
+
<ul>
|
167
|
+
<li>The life cycle of Active Record objects.</li>
|
168
|
+
<li>How to create callback methods that respond to events in the object life cycle.</li>
|
169
|
+
<li>How to create special classes that encapsulate common behavior for your callbacks.</li>
|
170
|
+
</ul>
|
171
|
+
|
172
|
+
|
173
|
+
<div id="subCol">
|
174
|
+
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
|
175
|
+
<ol class="chapters">
|
176
|
+
<li><a href="#the-object-life-cycle">The Object Life Cycle</a></li>
|
177
|
+
<li>
|
178
|
+
<a href="#callbacks-overview">Callbacks Overview</a>
|
179
|
+
|
180
|
+
<ul>
|
181
|
+
<li><a href="#callback-registration">Callback Registration</a></li>
|
182
|
+
</ul>
|
183
|
+
</li>
|
184
|
+
<li>
|
185
|
+
<a href="#available-callbacks">Available Callbacks</a>
|
186
|
+
|
187
|
+
<ul>
|
188
|
+
<li><a href="#creating-an-object">Creating an Object</a></li>
|
189
|
+
<li><a href="#updating-an-object">Updating an Object</a></li>
|
190
|
+
<li><a href="#destroying-an-object">Destroying an Object</a></li>
|
191
|
+
<li><a href="#after_initialize-and-after_find"><code>after_initialize</code> and <code>after_find</code></a></li>
|
192
|
+
<li><a href="#after_touch"><code>after_touch</code></a></li>
|
193
|
+
</ul>
|
194
|
+
</li>
|
195
|
+
<li><a href="#running-callbacks">Running Callbacks</a></li>
|
196
|
+
<li><a href="#skipping-callbacks">Skipping Callbacks</a></li>
|
197
|
+
<li><a href="#halting-execution">Halting Execution</a></li>
|
198
|
+
<li><a href="#relational-callbacks">Relational Callbacks</a></li>
|
199
|
+
<li>
|
200
|
+
<a href="#conditional-callbacks">Conditional Callbacks</a>
|
201
|
+
|
202
|
+
<ul>
|
203
|
+
<li><a href="#using-:if-and-:unless-with-a-symbol">Using <code>:if</code> and <code>:unless</code> with a <code>Symbol</code></a></li>
|
204
|
+
<li><a href="#using-:if-and-:unless-with-a-string">Using <code>:if</code> and <code>:unless</code> with a String</a></li>
|
205
|
+
<li><a href="#using-:if-and-:unless-with-a-proc">Using <code>:if</code> and <code>:unless</code> with a <code>Proc</code></a></li>
|
206
|
+
<li><a href="#multiple-conditions-for-callbacks">Multiple Conditions for Callbacks</a></li>
|
207
|
+
</ul>
|
208
|
+
</li>
|
209
|
+
<li><a href="#callback-classes">Callback Classes</a></li>
|
210
|
+
<li><a href="#transaction-callbacks">Transaction Callbacks</a></li>
|
211
|
+
</ol>
|
212
|
+
|
213
|
+
</div>
|
214
|
+
|
215
|
+
</div>
|
216
|
+
</div>
|
217
|
+
|
218
|
+
<div id="container">
|
219
|
+
<div class="wrapper">
|
220
|
+
<div id="mainCol">
|
221
|
+
<h3 id="the-object-life-cycle">1 The Object Life Cycle</h3><p>During the normal operation of a Rails application, objects may be created, updated, and destroyed. Active Record provides hooks into this <em>object life cycle</em> so that you can control your application and its data.</p><p>Callbacks allow you to trigger logic before or after an alteration of an object's state.</p><h3 id="callbacks-overview">2 Callbacks Overview</h3><p>Callbacks are methods that get called at certain moments of an object's life cycle. With callbacks it is possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.</p><h4 id="callback-registration">2.1 Callback Registration</h4><p>In order to use the available callbacks, you need to register them. You can implement the callbacks as ordinary methods and use a macro-style class method to register them as callbacks:</p><div class="code_container">
|
222
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
223
|
+
class User < ActiveRecord::Base
|
224
|
+
validates :login, :email, presence: true
|
225
|
+
|
226
|
+
before_validation :ensure_login_has_a_value
|
227
|
+
|
228
|
+
protected
|
229
|
+
def ensure_login_has_a_value
|
230
|
+
if login.nil?
|
231
|
+
self.login = email unless email.blank?
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
</pre>
|
237
|
+
</div>
|
238
|
+
<p>The macro-style class methods can also receive a block. Consider using this style if the code inside your block is so short that it fits in a single line:</p><div class="code_container">
|
239
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
240
|
+
class User < ActiveRecord::Base
|
241
|
+
validates :login, :email, presence: true
|
242
|
+
|
243
|
+
before_create do
|
244
|
+
self.name = login.capitalize if name.blank?
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
</pre>
|
249
|
+
</div>
|
250
|
+
<p>Callbacks can also be registered to only fire on certain life cycle events:</p><div class="code_container">
|
251
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
252
|
+
class User < ActiveRecord::Base
|
253
|
+
before_validation :normalize_name, on: :create
|
254
|
+
|
255
|
+
# :on takes an array as well
|
256
|
+
after_validation :set_location, on: [ :create, :update ]
|
257
|
+
|
258
|
+
protected
|
259
|
+
def normalize_name
|
260
|
+
self.name = self.name.downcase.titleize
|
261
|
+
end
|
262
|
+
|
263
|
+
def set_location
|
264
|
+
self.location = LocationService.query(self)
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
</pre>
|
269
|
+
</div>
|
270
|
+
<p>It is considered good practice to declare callback methods as protected or private. If left public, they can be called from outside of the model and violate the principle of object encapsulation.</p><h3 id="available-callbacks">3 Available Callbacks</h3><p>Here is a list with all the available Active Record callbacks, listed in the same order in which they will get called during the respective operations:</p><h4 id="creating-an-object">3.1 Creating an Object</h4>
|
271
|
+
<ul>
|
272
|
+
<li><code>before_validation</code></li>
|
273
|
+
<li><code>after_validation</code></li>
|
274
|
+
<li><code>before_save</code></li>
|
275
|
+
<li><code>around_save</code></li>
|
276
|
+
<li><code>before_create</code></li>
|
277
|
+
<li><code>around_create</code></li>
|
278
|
+
<li><code>after_create</code></li>
|
279
|
+
<li><code>after_save</code></li>
|
280
|
+
<li><code>after_commit/after_rollback</code></li>
|
281
|
+
</ul>
|
282
|
+
<h4 id="updating-an-object">3.2 Updating an Object</h4>
|
283
|
+
<ul>
|
284
|
+
<li><code>before_validation</code></li>
|
285
|
+
<li><code>after_validation</code></li>
|
286
|
+
<li><code>before_save</code></li>
|
287
|
+
<li><code>around_save</code></li>
|
288
|
+
<li><code>before_update</code></li>
|
289
|
+
<li><code>around_update</code></li>
|
290
|
+
<li><code>after_update</code></li>
|
291
|
+
<li><code>after_save</code></li>
|
292
|
+
<li><code>after_commit/after_rollback</code></li>
|
293
|
+
</ul>
|
294
|
+
<h4 id="destroying-an-object">3.3 Destroying an Object</h4>
|
295
|
+
<ul>
|
296
|
+
<li><code>before_destroy</code></li>
|
297
|
+
<li><code>around_destroy</code></li>
|
298
|
+
<li><code>after_destroy</code></li>
|
299
|
+
<li><code>after_commit/after_rollback</code></li>
|
300
|
+
</ul>
|
301
|
+
<div class="warning"><p><code>after_save</code> runs both on create and update, but always <em>after</em> the more specific callbacks <code>after_create</code> and <code>after_update</code>, no matter the order in which the macro calls were executed.</p></div><h4 id="after_initialize-and-after_find">3.4 <code>after_initialize</code> and <code>after_find</code>
|
302
|
+
</h4><p>The <code>after_initialize</code> callback will be called whenever an Active Record object is instantiated, either by directly using <code>new</code> or when a record is loaded from the database. It can be useful to avoid the need to directly override your Active Record <code>initialize</code> method.</p><p>The <code>after_find</code> callback will be called whenever Active Record loads a record from the database. <code>after_find</code> is called before <code>after_initialize</code> if both are defined.</p><p>The <code>after_initialize</code> and <code>after_find</code> callbacks have no <code>before_*</code> counterparts, but they can be registered just like the other Active Record callbacks.</p><div class="code_container">
|
303
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
304
|
+
class User < ActiveRecord::Base
|
305
|
+
after_initialize do |user|
|
306
|
+
puts "You have initialized an object!"
|
307
|
+
end
|
308
|
+
|
309
|
+
after_find do |user|
|
310
|
+
puts "You have found an object!"
|
311
|
+
end
|
312
|
+
end
|
313
|
+
|
314
|
+
>> User.new
|
315
|
+
You have initialized an object!
|
316
|
+
=> #<User id: nil>
|
317
|
+
|
318
|
+
>> User.first
|
319
|
+
You have found an object!
|
320
|
+
You have initialized an object!
|
321
|
+
=> #<User id: 1>
|
322
|
+
|
323
|
+
</pre>
|
324
|
+
</div>
|
325
|
+
<h4 id="after_touch">3.5 <code>after_touch</code>
|
326
|
+
</h4><p>The <code>after_touch</code> callback will be called whenever an Active Record object is touched.</p><div class="code_container">
|
327
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
328
|
+
class User < ActiveRecord::Base
|
329
|
+
after_touch do |user|
|
330
|
+
puts "You have touched an object"
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
>> u = User.create(name: 'Kuldeep')
|
335
|
+
=> #<User id: 1, name: "Kuldeep", created_at: "2013-11-25 12:17:49", updated_at: "2013-11-25 12:17:49">
|
336
|
+
|
337
|
+
>> u.touch
|
338
|
+
You have touched an object
|
339
|
+
=> true
|
340
|
+
|
341
|
+
</pre>
|
342
|
+
</div>
|
343
|
+
<p>It can be used along with <code>belongs_to</code>:</p><div class="code_container">
|
344
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
345
|
+
class Employee < ActiveRecord::Base
|
346
|
+
belongs_to :company, touch: true
|
347
|
+
after_touch do
|
348
|
+
puts 'An Employee was touched'
|
349
|
+
end
|
350
|
+
end
|
351
|
+
|
352
|
+
class Company < ActiveRecord::Base
|
353
|
+
has_many :employees
|
354
|
+
after_touch :log_when_employees_or_company_touched
|
355
|
+
|
356
|
+
private
|
357
|
+
def log_when_employees_or_company_touched
|
358
|
+
puts 'Employee/Company was touched'
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
>> @employee = Employee.last
|
363
|
+
=> #<Employee id: 1, company_id: 1, created_at: "2013-11-25 17:04:22", updated_at: "2013-11-25 17:05:05">
|
364
|
+
|
365
|
+
# triggers @employee.company.touch
|
366
|
+
>> @employee.touch
|
367
|
+
Employee/Company was touched
|
368
|
+
An Employee was touched
|
369
|
+
=> true
|
370
|
+
|
371
|
+
</pre>
|
372
|
+
</div>
|
373
|
+
<h3 id="running-callbacks">4 Running Callbacks</h3><p>The following methods trigger callbacks:</p>
|
374
|
+
<ul>
|
375
|
+
<li><code>create</code></li>
|
376
|
+
<li><code>create!</code></li>
|
377
|
+
<li><code>decrement!</code></li>
|
378
|
+
<li><code>destroy</code></li>
|
379
|
+
<li><code>destroy!</code></li>
|
380
|
+
<li><code>destroy_all</code></li>
|
381
|
+
<li><code>increment!</code></li>
|
382
|
+
<li><code>save</code></li>
|
383
|
+
<li><code>save!</code></li>
|
384
|
+
<li><code>save(validate: false)</code></li>
|
385
|
+
<li><code>toggle!</code></li>
|
386
|
+
<li><code>update_attribute</code></li>
|
387
|
+
<li><code>update</code></li>
|
388
|
+
<li><code>update!</code></li>
|
389
|
+
<li><code>valid?</code></li>
|
390
|
+
</ul>
|
391
|
+
<p>Additionally, the <code>after_find</code> callback is triggered by the following finder methods:</p>
|
392
|
+
<ul>
|
393
|
+
<li><code>all</code></li>
|
394
|
+
<li><code>first</code></li>
|
395
|
+
<li><code>find</code></li>
|
396
|
+
<li><code>find_by</code></li>
|
397
|
+
<li><code>find_by_*</code></li>
|
398
|
+
<li><code>find_by_*!</code></li>
|
399
|
+
<li><code>find_by_sql</code></li>
|
400
|
+
<li><code>last</code></li>
|
401
|
+
</ul>
|
402
|
+
<p>The <code>after_initialize</code> callback is triggered every time a new object of the class is initialized.</p><div class="note"><p>The <code>find_by_*</code> and <code>find_by_*!</code> methods are dynamic finders generated automatically for every attribute. Learn more about them at the <a href="active_record_querying.html#dynamic-finders">Dynamic finders section</a></p></div><h3 id="skipping-callbacks">5 Skipping Callbacks</h3><p>Just as with validations, it is also possible to skip callbacks by using the following methods:</p>
|
403
|
+
<ul>
|
404
|
+
<li><code>decrement</code></li>
|
405
|
+
<li><code>decrement_counter</code></li>
|
406
|
+
<li><code>delete</code></li>
|
407
|
+
<li><code>delete_all</code></li>
|
408
|
+
<li><code>increment</code></li>
|
409
|
+
<li><code>increment_counter</code></li>
|
410
|
+
<li><code>toggle</code></li>
|
411
|
+
<li><code>touch</code></li>
|
412
|
+
<li><code>update_column</code></li>
|
413
|
+
<li><code>update_columns</code></li>
|
414
|
+
<li><code>update_all</code></li>
|
415
|
+
<li><code>update_counters</code></li>
|
416
|
+
</ul>
|
417
|
+
<p>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.</p><h3 id="halting-execution">6 Halting Execution</h3><p>As you start registering new callbacks for your models, they will be queued for execution. This queue will include all your model's validations, the registered callbacks, and the database operation to be executed.</p><p>The whole callback chain is wrapped in a transaction. If any <em>before</em> callback method returns exactly <code>false</code> or raises an exception, the execution chain gets halted and a ROLLBACK is issued; <em>after</em> callbacks can only accomplish that by raising an exception.</p><div class="warning"><p>Any exception that is not <code>ActiveRecord::Rollback</code> will be re-raised by Rails after the callback chain is halted. Raising an exception other than <code>ActiveRecord::Rollback</code> may break code that does not expect methods like <code>save</code> and <code>update_attributes</code> (which normally try to return <code>true</code> or <code>false</code>) to raise an exception.</p></div><h3 id="relational-callbacks">7 Relational Callbacks</h3><p>Callbacks work through model relationships, and can even be defined by them. Suppose an example where a user has many articles. A user's articles should be destroyed if the user is destroyed. Let's add an <code>after_destroy</code> callback to the <code>User</code> model by way of its relationship to the <code>Article</code> model:</p><div class="code_container">
|
418
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
419
|
+
class User < ActiveRecord::Base
|
420
|
+
has_many :articles, dependent: :destroy
|
421
|
+
end
|
422
|
+
|
423
|
+
class Article < ActiveRecord::Base
|
424
|
+
after_destroy :log_destroy_action
|
425
|
+
|
426
|
+
def log_destroy_action
|
427
|
+
puts 'Article destroyed'
|
428
|
+
end
|
429
|
+
end
|
430
|
+
|
431
|
+
>> user = User.first
|
432
|
+
=> #<User id: 1>
|
433
|
+
>> user.articles.create!
|
434
|
+
=> #<Article id: 1, user_id: 1>
|
435
|
+
>> user.destroy
|
436
|
+
Article destroyed
|
437
|
+
=> #<User id: 1>
|
438
|
+
|
439
|
+
</pre>
|
440
|
+
</div>
|
441
|
+
<h3 id="conditional-callbacks">8 Conditional Callbacks</h3><p>As with validations, we can also make the calling of a callback method conditional on the satisfaction of a given predicate. We can do this using the <code>:if</code> and <code>:unless</code> options, which can take a symbol, a string, a <code>Proc</code> or an <code>Array</code>. You may use the <code>:if</code> option when you want to specify under which conditions the callback <strong>should</strong> be called. If you want to specify the conditions under which the callback <strong>should not</strong> be called, then you may use the <code>:unless</code> option.</p><h4 id="using-:if-and-:unless-with-a-symbol">8.1 Using <code>:if</code> and <code>:unless</code> with a <code>Symbol</code>
|
442
|
+
</h4><p>You can associate the <code>:if</code> and <code>:unless</code> options with a symbol corresponding to the name of a predicate method that will get called right before the callback. When using the <code>:if</code> option, the callback won't be executed if the predicate method returns false; when using the <code>:unless</code> option, the callback won't be executed if the predicate method returns true. This is the most common option. Using this form of registration it is also possible to register several different predicates that should be called to check if the callback should be executed.</p><div class="code_container">
|
443
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
444
|
+
class Order < ActiveRecord::Base
|
445
|
+
before_save :normalize_card_number, if: :paid_with_card?
|
446
|
+
end
|
447
|
+
|
448
|
+
</pre>
|
449
|
+
</div>
|
450
|
+
<h4 id="using-:if-and-:unless-with-a-string">8.2 Using <code>:if</code> and <code>:unless</code> with a String</h4><p>You can also use a string that will be evaluated using <code>eval</code> and hence needs to contain valid Ruby code. You should use this option only when the string represents a really short condition:</p><div class="code_container">
|
451
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
452
|
+
class Order < ActiveRecord::Base
|
453
|
+
before_save :normalize_card_number, if: "paid_with_card?"
|
454
|
+
end
|
455
|
+
|
456
|
+
</pre>
|
457
|
+
</div>
|
458
|
+
<h4 id="using-:if-and-:unless-with-a-proc">8.3 Using <code>:if</code> and <code>:unless</code> with a <code>Proc</code>
|
459
|
+
</h4><p>Finally, it is possible to associate <code>:if</code> and <code>:unless</code> with a <code>Proc</code> object. This option is best suited when writing short validation methods, usually one-liners:</p><div class="code_container">
|
460
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
461
|
+
class Order < ActiveRecord::Base
|
462
|
+
before_save :normalize_card_number,
|
463
|
+
if: Proc.new { |order| order.paid_with_card? }
|
464
|
+
end
|
465
|
+
|
466
|
+
</pre>
|
467
|
+
</div>
|
468
|
+
<h4 id="multiple-conditions-for-callbacks">8.4 Multiple Conditions for Callbacks</h4><p>When writing conditional callbacks, it is possible to mix both <code>:if</code> and <code>:unless</code> in the same callback declaration:</p><div class="code_container">
|
469
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
470
|
+
class Comment < ActiveRecord::Base
|
471
|
+
after_create :send_email_to_author, if: :author_wants_emails?,
|
472
|
+
unless: Proc.new { |comment| comment.article.ignore_comments? }
|
473
|
+
end
|
474
|
+
|
475
|
+
</pre>
|
476
|
+
</div>
|
477
|
+
<h3 id="callback-classes">9 Callback Classes</h3><p>Sometimes the callback methods that you'll write will be useful enough to be reused by other models. Active Record makes it possible to create classes that encapsulate the callback methods, so it becomes very easy to reuse them.</p><p>Here's an example where we create a class with an <code>after_destroy</code> callback for a <code>PictureFile</code> model:</p><div class="code_container">
|
478
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
479
|
+
class PictureFileCallbacks
|
480
|
+
def after_destroy(picture_file)
|
481
|
+
if File.exist?(picture_file.filepath)
|
482
|
+
File.delete(picture_file.filepath)
|
483
|
+
end
|
484
|
+
end
|
485
|
+
end
|
486
|
+
|
487
|
+
</pre>
|
488
|
+
</div>
|
489
|
+
<p>When declared inside a class, as above, the callback methods will receive the model object as a parameter. We can now use the callback class in the model:</p><div class="code_container">
|
490
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
491
|
+
class PictureFile < ActiveRecord::Base
|
492
|
+
after_destroy PictureFileCallbacks.new
|
493
|
+
end
|
494
|
+
|
495
|
+
</pre>
|
496
|
+
</div>
|
497
|
+
<p>Note that we needed to instantiate a new <code>PictureFileCallbacks</code> object, since we declared our callback as an instance method. This is particularly useful if the callbacks make use of the state of the instantiated object. Often, however, it will make more sense to declare the callbacks as class methods:</p><div class="code_container">
|
498
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
499
|
+
class PictureFileCallbacks
|
500
|
+
def self.after_destroy(picture_file)
|
501
|
+
if File.exist?(picture_file.filepath)
|
502
|
+
File.delete(picture_file.filepath)
|
503
|
+
end
|
504
|
+
end
|
505
|
+
end
|
506
|
+
|
507
|
+
</pre>
|
508
|
+
</div>
|
509
|
+
<p>If the callback method is declared this way, it won't be necessary to instantiate a <code>PictureFileCallbacks</code> object.</p><div class="code_container">
|
510
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
511
|
+
class PictureFile < ActiveRecord::Base
|
512
|
+
after_destroy PictureFileCallbacks
|
513
|
+
end
|
514
|
+
|
515
|
+
</pre>
|
516
|
+
</div>
|
517
|
+
<p>You can declare as many callbacks as you want inside your callback classes.</p><h3 id="transaction-callbacks">10 Transaction Callbacks</h3><p>There are two additional callbacks that are triggered by the completion of a database transaction: <code>after_commit</code> and <code>after_rollback</code>. These callbacks are very similar to the <code>after_save</code> callback except that they don't execute until after database changes have either been committed or rolled back. They are most useful when your active record models need to interact with external systems which are not part of the database transaction.</p><p>Consider, for example, the previous example where the <code>PictureFile</code> model needs to delete a file after the corresponding record is destroyed. If anything raises an exception after the <code>after_destroy</code> callback is called and the transaction rolls back, the file will have been deleted and the model will be left in an inconsistent state. For example, suppose that <code>picture_file_2</code> in the code below is not valid and the <code>save!</code> method raises an error.</p><div class="code_container">
|
518
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
519
|
+
PictureFile.transaction do
|
520
|
+
picture_file_1.destroy
|
521
|
+
picture_file_2.save!
|
522
|
+
end
|
523
|
+
|
524
|
+
</pre>
|
525
|
+
</div>
|
526
|
+
<p>By using the <code>after_commit</code> callback we can account for this case.</p><div class="code_container">
|
527
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
528
|
+
class PictureFile < ActiveRecord::Base
|
529
|
+
after_commit :delete_picture_file_from_disk, on: [:destroy]
|
530
|
+
|
531
|
+
def delete_picture_file_from_disk
|
532
|
+
if File.exist?(filepath)
|
533
|
+
File.delete(filepath)
|
534
|
+
end
|
535
|
+
end
|
536
|
+
end
|
537
|
+
|
538
|
+
</pre>
|
539
|
+
</div>
|
540
|
+
<div class="note"><p>the <code>:on</code> option specifies when a callback will be fired. If you
|
541
|
+
don't supply the <code>:on</code> option the callback will fire for every action.</p></div><div class="warning"><p>The <code>after_commit</code> and <code>after_rollback</code> 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.</p></div>
|
542
|
+
|
543
|
+
<h3>Feedback</h3>
|
544
|
+
<p>
|
545
|
+
You're encouraged to help improve the quality of this guide.
|
546
|
+
</p>
|
547
|
+
<p>
|
548
|
+
Please contribute if you see any typos or factual errors.
|
549
|
+
To get started, you can read our <a href="http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">documentation contributions</a> section.
|
550
|
+
</p>
|
551
|
+
<p>
|
552
|
+
You may also find incomplete content, or stuff that is not up to date.
|
553
|
+
Please do add any missing documentation for master. Make sure to check
|
554
|
+
<a href="http://edgeguides.rubyonrails.org">Edge Guides</a> first to verify
|
555
|
+
if the issues are already fixed or not on the master branch.
|
556
|
+
Check the <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
|
557
|
+
for style and conventions.
|
558
|
+
</p>
|
559
|
+
<p>
|
560
|
+
If for whatever reason you spot something to fix but cannot patch it yourself, please
|
561
|
+
<a href="https://github.com/rails/rails/issues">open an issue</a>.
|
562
|
+
</p>
|
563
|
+
<p>And last but not least, any kind of discussion regarding Ruby on Rails
|
564
|
+
documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
|
565
|
+
</p>
|
566
|
+
</div>
|
567
|
+
</div>
|
568
|
+
</div>
|
569
|
+
|
570
|
+
<hr class="hide" />
|
571
|
+
<div id="footer">
|
572
|
+
<div class="wrapper">
|
573
|
+
<p>This work is licensed under a <a href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International</a> License</p>
|
574
|
+
<p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
|
575
|
+
|
576
|
+
</div>
|
577
|
+
</div>
|
578
|
+
|
579
|
+
<script type="text/javascript" src="javascripts/jquery.min.js"></script>
|
580
|
+
<script type="text/javascript" src="javascripts/responsive-tables.js"></script>
|
581
|
+
<script type="text/javascript" src="javascripts/guides.js"></script>
|
582
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
|
583
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
|
584
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
|
585
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
|
586
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
|
587
|
+
<script type="text/javascript">
|
588
|
+
SyntaxHighlighter.all();
|
589
|
+
$(guidesIndex.bind);
|
590
|
+
</script>
|
591
|
+
</body>
|
592
|
+
</html>
|