rails 4.1.4 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -18
- metadata +51 -304
- data/guides/CHANGELOG.md +0 -41
- data/guides/Rakefile +0 -77
- data/guides/assets/images/akshaysurve.jpg +0 -0
- data/guides/assets/images/belongs_to.png +0 -0
- data/guides/assets/images/book_icon.gif +0 -0
- data/guides/assets/images/bullet.gif +0 -0
- data/guides/assets/images/chapters_icon.gif +0 -0
- data/guides/assets/images/check_bullet.gif +0 -0
- data/guides/assets/images/credits_pic_blank.gif +0 -0
- data/guides/assets/images/csrf.png +0 -0
- data/guides/assets/images/edge_badge.png +0 -0
- data/guides/assets/images/favicon.ico +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/grey_bullet.gif +0 -0
- data/guides/assets/images/habtm.png +0 -0
- data/guides/assets/images/has_many.png +0 -0
- data/guides/assets/images/has_many_through.png +0 -0
- data/guides/assets/images/has_one.png +0 -0
- data/guides/assets/images/has_one_through.png +0 -0
- data/guides/assets/images/header_backdrop.png +0 -0
- data/guides/assets/images/header_tile.gif +0 -0
- data/guides/assets/images/i18n/demo_html_safe.png +0 -0
- data/guides/assets/images/i18n/demo_localized_pirate.png +0 -0
- data/guides/assets/images/i18n/demo_translated_en.png +0 -0
- data/guides/assets/images/i18n/demo_translated_pirate.png +0 -0
- data/guides/assets/images/i18n/demo_translation_missing.png +0 -0
- data/guides/assets/images/i18n/demo_untranslated.png +0 -0
- data/guides/assets/images/icons/README +0 -5
- data/guides/assets/images/icons/callouts/1.png +0 -0
- data/guides/assets/images/icons/callouts/10.png +0 -0
- 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/14.png +0 -0
- data/guides/assets/images/icons/callouts/15.png +0 -0
- data/guides/assets/images/icons/callouts/2.png +0 -0
- data/guides/assets/images/icons/callouts/3.png +0 -0
- data/guides/assets/images/icons/callouts/4.png +0 -0
- data/guides/assets/images/icons/callouts/5.png +0 -0
- data/guides/assets/images/icons/callouts/6.png +0 -0
- data/guides/assets/images/icons/callouts/7.png +0 -0
- data/guides/assets/images/icons/callouts/8.png +0 -0
- data/guides/assets/images/icons/callouts/9.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/icons/home.png +0 -0
- data/guides/assets/images/icons/important.png +0 -0
- data/guides/assets/images/icons/next.png +0 -0
- data/guides/assets/images/icons/note.png +0 -0
- data/guides/assets/images/icons/prev.png +0 -0
- data/guides/assets/images/icons/tip.png +0 -0
- data/guides/assets/images/icons/up.png +0 -0
- data/guides/assets/images/icons/warning.png +0 -0
- data/guides/assets/images/nav_arrow.gif +0 -0
- data/guides/assets/images/oscardelben.jpg +0 -0
- data/guides/assets/images/polymorphic.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/rails_guides_logo.gif +0 -0
- data/guides/assets/images/rails_logo_remix.gif +0 -0
- data/guides/assets/images/session_fixation.png +0 -0
- data/guides/assets/images/tab_grey.gif +0 -0
- data/guides/assets/images/tab_info.gif +0 -0
- data/guides/assets/images/tab_note.gif +0 -0
- data/guides/assets/images/tab_red.gif +0 -0
- data/guides/assets/images/tab_yellow.gif +0 -0
- data/guides/assets/images/tab_yellow.png +0 -0
- data/guides/assets/images/vijaydev.jpg +0 -0
- data/guides/assets/javascripts/guides.js +0 -53
- data/guides/assets/javascripts/jquery.min.js +0 -4
- data/guides/assets/javascripts/responsive-tables.js +0 -43
- data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
- data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
- data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
- data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
- data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
- data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
- data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
- data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
- data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
- data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
- data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
- data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
- data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
- data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
- data/guides/assets/javascripts/syntaxhighlighter/shCore.js +0 -17
- data/guides/assets/stylesheets/fixes.css +0 -16
- data/guides/assets/stylesheets/kindle.css +0 -11
- data/guides/assets/stylesheets/main.css +0 -710
- data/guides/assets/stylesheets/print.css +0 -52
- data/guides/assets/stylesheets/reset.css +0 -43
- data/guides/assets/stylesheets/responsive-tables.css +0 -50
- data/guides/assets/stylesheets/style.css +0 -13
- data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +0 -226
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
- data/guides/bug_report_templates/action_controller_gem.rb +0 -47
- data/guides/bug_report_templates/action_controller_master.rb +0 -53
- data/guides/bug_report_templates/active_record_gem.rb +0 -40
- data/guides/bug_report_templates/active_record_master.rb +0 -49
- data/guides/code/getting_started/Gemfile +0 -40
- data/guides/code/getting_started/Gemfile.lock +0 -125
- data/guides/code/getting_started/README.rdoc +0 -28
- data/guides/code/getting_started/Rakefile +0 -6
- data/guides/code/getting_started/app/assets/javascripts/application.js +0 -15
- data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/application.css +0 -13
- data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +0 -3
- data/guides/code/getting_started/app/controllers/application_controller.rb +0 -5
- data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -23
- data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -53
- data/guides/code/getting_started/app/controllers/welcome_controller.rb +0 -4
- data/guides/code/getting_started/app/helpers/application_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/comments_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/posts_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/welcome_helper.rb +0 -2
- data/guides/code/getting_started/app/models/comment.rb +0 -3
- data/guides/code/getting_started/app/models/post.rb +0 -7
- data/guides/code/getting_started/app/views/comments/_comment.html.erb +0 -15
- data/guides/code/getting_started/app/views/comments/_form.html.erb +0 -13
- data/guides/code/getting_started/app/views/layouts/application.html.erb +0 -14
- data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -27
- data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/index.html.erb +0 -21
- data/guides/code/getting_started/app/views/posts/new.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/show.html.erb +0 -18
- data/guides/code/getting_started/app/views/welcome/index.html.erb +0 -4
- data/guides/code/getting_started/bin/bundle +0 -4
- data/guides/code/getting_started/bin/rails +0 -4
- data/guides/code/getting_started/bin/rake +0 -4
- data/guides/code/getting_started/config/application.rb +0 -18
- data/guides/code/getting_started/config/boot.rb +0 -4
- data/guides/code/getting_started/config/database.yml +0 -25
- data/guides/code/getting_started/config/environment.rb +0 -5
- data/guides/code/getting_started/config/environments/development.rb +0 -30
- data/guides/code/getting_started/config/environments/production.rb +0 -80
- data/guides/code/getting_started/config/environments/test.rb +0 -36
- data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
- data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +0 -4
- data/guides/code/getting_started/config/initializers/inflections.rb +0 -16
- data/guides/code/getting_started/config/initializers/locale.rb +0 -9
- data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
- data/guides/code/getting_started/config/initializers/secret_token.rb +0 -12
- data/guides/code/getting_started/config/initializers/session_store.rb +0 -3
- data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
- data/guides/code/getting_started/config/locales/en.yml +0 -23
- data/guides/code/getting_started/config/routes.rb +0 -7
- data/guides/code/getting_started/config.ru +0 -4
- data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +0 -10
- data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +0 -11
- data/guides/code/getting_started/db/schema.rb +0 -33
- data/guides/code/getting_started/db/seeds.rb +0 -7
- data/guides/code/getting_started/public/404.html +0 -60
- data/guides/code/getting_started/public/422.html +0 -60
- data/guides/code/getting_started/public/500.html +0 -59
- data/guides/code/getting_started/public/favicon.ico +0 -0
- data/guides/code/getting_started/public/robots.txt +0 -5
- data/guides/code/getting_started/test/controllers/comments_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/posts_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +0 -9
- data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
- data/guides/code/getting_started/test/fixtures/posts.yml +0 -9
- data/guides/code/getting_started/test/helpers/comments_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/posts_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +0 -4
- data/guides/code/getting_started/test/models/comment_test.rb +0 -7
- data/guides/code/getting_started/test/models/post_test.rb +0 -7
- data/guides/code/getting_started/test/test_helper.rb +0 -12
- data/guides/rails_guides/generator.rb +0 -248
- data/guides/rails_guides/helpers.rb +0 -53
- data/guides/rails_guides/indexer.rb +0 -68
- data/guides/rails_guides/kindle.rb +0 -119
- data/guides/rails_guides/levenshtein.rb +0 -31
- data/guides/rails_guides/markdown/renderer.rb +0 -82
- data/guides/rails_guides/markdown.rb +0 -163
- data/guides/rails_guides.rb +0 -63
- data/guides/source/2_2_release_notes.md +0 -435
- data/guides/source/2_3_release_notes.md +0 -621
- data/guides/source/3_0_release_notes.md +0 -611
- data/guides/source/3_1_release_notes.md +0 -556
- data/guides/source/3_2_release_notes.md +0 -565
- data/guides/source/4_0_release_notes.md +0 -276
- data/guides/source/4_1_release_notes.md +0 -731
- data/guides/source/_license.html.erb +0 -2
- data/guides/source/_welcome.html.erb +0 -25
- data/guides/source/action_controller_overview.md +0 -1175
- data/guides/source/action_mailer_basics.md +0 -689
- data/guides/source/action_view_overview.md +0 -1610
- data/guides/source/active_model_basics.md +0 -200
- data/guides/source/active_record_basics.md +0 -373
- data/guides/source/active_record_callbacks.md +0 -410
- data/guides/source/active_record_querying.md +0 -1773
- data/guides/source/active_record_validations.md +0 -1169
- data/guides/source/active_support_core_extensions.md +0 -3864
- data/guides/source/active_support_instrumentation.md +0 -496
- data/guides/source/api_documentation_guidelines.md +0 -315
- data/guides/source/asset_pipeline.md +0 -1163
- data/guides/source/association_basics.md +0 -2229
- data/guides/source/caching_with_rails.md +0 -354
- data/guides/source/command_line.md +0 -603
- data/guides/source/configuring.md +0 -942
- data/guides/source/contributing_to_ruby_on_rails.md +0 -544
- data/guides/source/credits.html.erb +0 -80
- data/guides/source/debugging_rails_applications.md +0 -707
- data/guides/source/development_dependencies_install.md +0 -278
- data/guides/source/documents.yaml +0 -193
- data/guides/source/engines.md +0 -1391
- data/guides/source/form_helpers.md +0 -1001
- data/guides/source/generators.md +0 -663
- data/guides/source/getting_started.md +0 -1947
- data/guides/source/i18n.md +0 -1043
- data/guides/source/index.html.erb +0 -27
- data/guides/source/initialization.md +0 -657
- data/guides/source/kindle/copyright.html.erb +0 -1
- data/guides/source/kindle/layout.html.erb +0 -27
- data/guides/source/kindle/rails_guides.opf.erb +0 -52
- data/guides/source/kindle/toc.html.erb +0 -24
- data/guides/source/kindle/toc.ncx.erb +0 -64
- data/guides/source/kindle/welcome.html.erb +0 -5
- data/guides/source/layout.html.erb +0 -146
- data/guides/source/layouts_and_rendering.md +0 -1226
- data/guides/source/maintenance_policy.md +0 -56
- data/guides/source/migrations.md +0 -1109
- data/guides/source/nested_model_forms.md +0 -225
- data/guides/source/plugins.md +0 -444
- data/guides/source/rails_application_templates.md +0 -248
- data/guides/source/rails_on_rack.md +0 -333
- data/guides/source/routing.md +0 -1115
- data/guides/source/ruby_on_rails_guides_guidelines.md +0 -128
- data/guides/source/security.md +0 -1018
- data/guides/source/testing.md +0 -1052
- data/guides/source/upgrading_ruby_on_rails.md +0 -899
- data/guides/source/working_with_javascript_in_rails.md +0 -405
- data/guides/w3c_validator.rb +0 -95
@@ -1,410 +0,0 @@
|
|
1
|
-
Active Record Callbacks
|
2
|
-
=======================
|
3
|
-
|
4
|
-
This guide teaches you how to hook into the life cycle of your Active Record
|
5
|
-
objects.
|
6
|
-
|
7
|
-
After reading this guide, you will know:
|
8
|
-
|
9
|
-
* The life cycle of Active Record objects.
|
10
|
-
* How to create callback methods that respond to events in the object life cycle.
|
11
|
-
* How to create special classes that encapsulate common behavior for your callbacks.
|
12
|
-
|
13
|
-
--------------------------------------------------------------------------------
|
14
|
-
|
15
|
-
The Object Life Cycle
|
16
|
-
---------------------
|
17
|
-
|
18
|
-
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.
|
19
|
-
|
20
|
-
Callbacks allow you to trigger logic before or after an alteration of an object's state.
|
21
|
-
|
22
|
-
Callbacks Overview
|
23
|
-
------------------
|
24
|
-
|
25
|
-
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.
|
26
|
-
|
27
|
-
### Callback Registration
|
28
|
-
|
29
|
-
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:
|
30
|
-
|
31
|
-
```ruby
|
32
|
-
class User < ActiveRecord::Base
|
33
|
-
validates :login, :email, presence: true
|
34
|
-
|
35
|
-
before_validation :ensure_login_has_a_value
|
36
|
-
|
37
|
-
protected
|
38
|
-
def ensure_login_has_a_value
|
39
|
-
if login.nil?
|
40
|
-
self.login = email unless email.blank?
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
```
|
45
|
-
|
46
|
-
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:
|
47
|
-
|
48
|
-
```ruby
|
49
|
-
class User < ActiveRecord::Base
|
50
|
-
validates :login, :email, presence: true
|
51
|
-
|
52
|
-
before_create do
|
53
|
-
self.name = login.capitalize if name.blank?
|
54
|
-
end
|
55
|
-
end
|
56
|
-
```
|
57
|
-
|
58
|
-
Callbacks can also be registered to only fire on certain life cycle events:
|
59
|
-
|
60
|
-
```ruby
|
61
|
-
class User < ActiveRecord::Base
|
62
|
-
before_validation :normalize_name, on: :create
|
63
|
-
|
64
|
-
# :on takes an array as well
|
65
|
-
after_validation :set_location, on: [ :create, :update ]
|
66
|
-
|
67
|
-
protected
|
68
|
-
def normalize_name
|
69
|
-
self.name = self.name.downcase.titleize
|
70
|
-
end
|
71
|
-
|
72
|
-
def set_location
|
73
|
-
self.location = LocationService.query(self)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
```
|
77
|
-
|
78
|
-
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.
|
79
|
-
|
80
|
-
Available Callbacks
|
81
|
-
-------------------
|
82
|
-
|
83
|
-
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:
|
84
|
-
|
85
|
-
### Creating an Object
|
86
|
-
|
87
|
-
* `before_validation`
|
88
|
-
* `after_validation`
|
89
|
-
* `before_save`
|
90
|
-
* `around_save`
|
91
|
-
* `before_create`
|
92
|
-
* `around_create`
|
93
|
-
* `after_create`
|
94
|
-
* `after_save`
|
95
|
-
|
96
|
-
### Updating an Object
|
97
|
-
|
98
|
-
* `before_validation`
|
99
|
-
* `after_validation`
|
100
|
-
* `before_save`
|
101
|
-
* `around_save`
|
102
|
-
* `before_update`
|
103
|
-
* `around_update`
|
104
|
-
* `after_update`
|
105
|
-
* `after_save`
|
106
|
-
|
107
|
-
### Destroying an Object
|
108
|
-
|
109
|
-
* `before_destroy`
|
110
|
-
* `around_destroy`
|
111
|
-
* `after_destroy`
|
112
|
-
|
113
|
-
WARNING. `after_save` runs both on create and update, but always _after_ the more specific callbacks `after_create` and `after_update`, no matter the order in which the macro calls were executed.
|
114
|
-
|
115
|
-
### `after_initialize` and `after_find`
|
116
|
-
|
117
|
-
The `after_initialize` callback will be called whenever an Active Record object is instantiated, either by directly using `new` or when a record is loaded from the database. It can be useful to avoid the need to directly override your Active Record `initialize` method.
|
118
|
-
|
119
|
-
The `after_find` callback will be called whenever Active Record loads a record from the database. `after_find` is called before `after_initialize` if both are defined.
|
120
|
-
|
121
|
-
The `after_initialize` and `after_find` callbacks have no `before_*` counterparts, but they can be registered just like the other Active Record callbacks.
|
122
|
-
|
123
|
-
```ruby
|
124
|
-
class User < ActiveRecord::Base
|
125
|
-
after_initialize do |user|
|
126
|
-
puts "You have initialized an object!"
|
127
|
-
end
|
128
|
-
|
129
|
-
after_find do |user|
|
130
|
-
puts "You have found an object!"
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
>> User.new
|
135
|
-
You have initialized an object!
|
136
|
-
=> #<User id: nil>
|
137
|
-
|
138
|
-
>> User.first
|
139
|
-
You have found an object!
|
140
|
-
You have initialized an object!
|
141
|
-
=> #<User id: 1>
|
142
|
-
```
|
143
|
-
|
144
|
-
### `after_touch`
|
145
|
-
|
146
|
-
The `after_touch` callback will be called whenever an Active Record object is touched.
|
147
|
-
|
148
|
-
```ruby
|
149
|
-
class User < ActiveRecord::Base
|
150
|
-
after_touch do |user|
|
151
|
-
puts "You have touched an object"
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
>> u = User.create(name: 'Kuldeep')
|
156
|
-
=> #<User id: 1, name: "Kuldeep", created_at: "2013-11-25 12:17:49", updated_at: "2013-11-25 12:17:49">
|
157
|
-
|
158
|
-
>> u.touch
|
159
|
-
You have touched an object
|
160
|
-
=> true
|
161
|
-
```
|
162
|
-
|
163
|
-
It can be used along with `belongs_to`:
|
164
|
-
|
165
|
-
```ruby
|
166
|
-
class Employee < ActiveRecord::Base
|
167
|
-
belongs_to :company, touch: true
|
168
|
-
after_touch do
|
169
|
-
puts 'An Employee was touched'
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
class Company < ActiveRecord::Base
|
174
|
-
has_many :employees
|
175
|
-
after_touch :log_when_employees_or_company_touched
|
176
|
-
|
177
|
-
private
|
178
|
-
def log_when_employees_or_company_touched
|
179
|
-
puts 'Employee/Company was touched'
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
183
|
-
>> @employee = Employee.last
|
184
|
-
=> #<Employee id: 1, company_id: 1, created_at: "2013-11-25 17:04:22", updated_at: "2013-11-25 17:05:05">
|
185
|
-
|
186
|
-
# triggers @employee.company.touch
|
187
|
-
>> @employee.touch
|
188
|
-
Employee/Company was touched
|
189
|
-
An Employee was touched
|
190
|
-
=> true
|
191
|
-
```
|
192
|
-
|
193
|
-
Running Callbacks
|
194
|
-
-----------------
|
195
|
-
|
196
|
-
The following methods trigger callbacks:
|
197
|
-
|
198
|
-
* `create`
|
199
|
-
* `create!`
|
200
|
-
* `decrement!`
|
201
|
-
* `destroy`
|
202
|
-
* `destroy!`
|
203
|
-
* `destroy_all`
|
204
|
-
* `increment!`
|
205
|
-
* `save`
|
206
|
-
* `save!`
|
207
|
-
* `save(validate: false)`
|
208
|
-
* `toggle!`
|
209
|
-
* `update_attribute`
|
210
|
-
* `update`
|
211
|
-
* `update!`
|
212
|
-
* `valid?`
|
213
|
-
|
214
|
-
Additionally, the `after_find` callback is triggered by the following finder methods:
|
215
|
-
|
216
|
-
* `all`
|
217
|
-
* `first`
|
218
|
-
* `find`
|
219
|
-
* `find_by`
|
220
|
-
* `find_by_*`
|
221
|
-
* `find_by_*!`
|
222
|
-
* `find_by_sql`
|
223
|
-
* `last`
|
224
|
-
|
225
|
-
The `after_initialize` callback is triggered every time a new object of the class is initialized.
|
226
|
-
|
227
|
-
NOTE: The `find_by_*` and `find_by_*!` methods are dynamic finders generated automatically for every attribute. Learn more about them at the [Dynamic finders section](active_record_querying.html#dynamic-finders)
|
228
|
-
|
229
|
-
Skipping Callbacks
|
230
|
-
------------------
|
231
|
-
|
232
|
-
Just as with validations, it is also possible to skip callbacks by using the following methods:
|
233
|
-
|
234
|
-
* `decrement`
|
235
|
-
* `decrement_counter`
|
236
|
-
* `delete`
|
237
|
-
* `delete_all`
|
238
|
-
* `increment`
|
239
|
-
* `increment_counter`
|
240
|
-
* `toggle`
|
241
|
-
* `touch`
|
242
|
-
* `update_column`
|
243
|
-
* `update_columns`
|
244
|
-
* `update_all`
|
245
|
-
* `update_counters`
|
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
|
-
|
249
|
-
Halting Execution
|
250
|
-
-----------------
|
251
|
-
|
252
|
-
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.
|
253
|
-
|
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.
|
255
|
-
|
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.
|
257
|
-
|
258
|
-
Relational Callbacks
|
259
|
-
--------------------
|
260
|
-
|
261
|
-
Callbacks work through model relationships, and can even be defined by them. Suppose an example where a user has many posts. A user's posts should be destroyed if the user is destroyed. Let's add an `after_destroy` callback to the `User` model by way of its relationship to the `Post` model:
|
262
|
-
|
263
|
-
```ruby
|
264
|
-
class User < ActiveRecord::Base
|
265
|
-
has_many :posts, dependent: :destroy
|
266
|
-
end
|
267
|
-
|
268
|
-
class Post < ActiveRecord::Base
|
269
|
-
after_destroy :log_destroy_action
|
270
|
-
|
271
|
-
def log_destroy_action
|
272
|
-
puts 'Post destroyed'
|
273
|
-
end
|
274
|
-
end
|
275
|
-
|
276
|
-
>> user = User.first
|
277
|
-
=> #<User id: 1>
|
278
|
-
>> user.posts.create!
|
279
|
-
=> #<Post id: 1, user_id: 1>
|
280
|
-
>> user.destroy
|
281
|
-
Post destroyed
|
282
|
-
=> #<User id: 1>
|
283
|
-
```
|
284
|
-
|
285
|
-
Conditional Callbacks
|
286
|
-
---------------------
|
287
|
-
|
288
|
-
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 `:if` and `:unless` options, which can take a symbol, a string, a `Proc` or an `Array`. You may use the `:if` option when you want to specify under which conditions the callback **should** be called. If you want to specify the conditions under which the callback **should not** be called, then you may use the `:unless` option.
|
289
|
-
|
290
|
-
### Using `:if` and `:unless` with a `Symbol`
|
291
|
-
|
292
|
-
You can associate the `:if` and `:unless` options with a symbol corresponding to the name of a predicate method that will get called right before the callback. When using the `:if` option, the callback won't be executed if the predicate method returns false; when using the `:unless` 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.
|
293
|
-
|
294
|
-
```ruby
|
295
|
-
class Order < ActiveRecord::Base
|
296
|
-
before_save :normalize_card_number, if: :paid_with_card?
|
297
|
-
end
|
298
|
-
```
|
299
|
-
|
300
|
-
### Using `:if` and `:unless` with a String
|
301
|
-
|
302
|
-
You can also use a string that will be evaluated using `eval` and hence needs to contain valid Ruby code. You should use this option only when the string represents a really short condition:
|
303
|
-
|
304
|
-
```ruby
|
305
|
-
class Order < ActiveRecord::Base
|
306
|
-
before_save :normalize_card_number, if: "paid_with_card?"
|
307
|
-
end
|
308
|
-
```
|
309
|
-
|
310
|
-
### Using `:if` and `:unless` with a `Proc`
|
311
|
-
|
312
|
-
Finally, it is possible to associate `:if` and `:unless` with a `Proc` object. This option is best suited when writing short validation methods, usually one-liners:
|
313
|
-
|
314
|
-
```ruby
|
315
|
-
class Order < ActiveRecord::Base
|
316
|
-
before_save :normalize_card_number,
|
317
|
-
if: Proc.new { |order| order.paid_with_card? }
|
318
|
-
end
|
319
|
-
```
|
320
|
-
|
321
|
-
### Multiple Conditions for Callbacks
|
322
|
-
|
323
|
-
When writing conditional callbacks, it is possible to mix both `:if` and `:unless` in the same callback declaration:
|
324
|
-
|
325
|
-
```ruby
|
326
|
-
class Comment < ActiveRecord::Base
|
327
|
-
after_create :send_email_to_author, if: :author_wants_emails?,
|
328
|
-
unless: Proc.new { |comment| comment.post.ignore_comments? }
|
329
|
-
end
|
330
|
-
```
|
331
|
-
|
332
|
-
Callback Classes
|
333
|
-
----------------
|
334
|
-
|
335
|
-
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.
|
336
|
-
|
337
|
-
Here's an example where we create a class with an `after_destroy` callback for a `PictureFile` model:
|
338
|
-
|
339
|
-
```ruby
|
340
|
-
class PictureFileCallbacks
|
341
|
-
def after_destroy(picture_file)
|
342
|
-
if File.exist?(picture_file.filepath)
|
343
|
-
File.delete(picture_file.filepath)
|
344
|
-
end
|
345
|
-
end
|
346
|
-
end
|
347
|
-
```
|
348
|
-
|
349
|
-
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:
|
350
|
-
|
351
|
-
```ruby
|
352
|
-
class PictureFile < ActiveRecord::Base
|
353
|
-
after_destroy PictureFileCallbacks.new
|
354
|
-
end
|
355
|
-
```
|
356
|
-
|
357
|
-
Note that we needed to instantiate a new `PictureFileCallbacks` 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:
|
358
|
-
|
359
|
-
```ruby
|
360
|
-
class PictureFileCallbacks
|
361
|
-
def self.after_destroy(picture_file)
|
362
|
-
if File.exist?(picture_file.filepath)
|
363
|
-
File.delete(picture_file.filepath)
|
364
|
-
end
|
365
|
-
end
|
366
|
-
end
|
367
|
-
```
|
368
|
-
|
369
|
-
If the callback method is declared this way, it won't be necessary to instantiate a `PictureFileCallbacks` object.
|
370
|
-
|
371
|
-
```ruby
|
372
|
-
class PictureFile < ActiveRecord::Base
|
373
|
-
after_destroy PictureFileCallbacks
|
374
|
-
end
|
375
|
-
```
|
376
|
-
|
377
|
-
You can declare as many callbacks as you want inside your callback classes.
|
378
|
-
|
379
|
-
Transaction Callbacks
|
380
|
-
---------------------
|
381
|
-
|
382
|
-
There are two additional callbacks that are triggered by the completion of a database transaction: `after_commit` and `after_rollback`. These callbacks are very similar to the `after_save` 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.
|
383
|
-
|
384
|
-
Consider, for example, the previous example where the `PictureFile` model needs to delete a file after the corresponding record is destroyed. If anything raises an exception after the `after_destroy` 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 `picture_file_2` in the code below is not valid and the `save!` method raises an error.
|
385
|
-
|
386
|
-
```ruby
|
387
|
-
PictureFile.transaction do
|
388
|
-
picture_file_1.destroy
|
389
|
-
picture_file_2.save!
|
390
|
-
end
|
391
|
-
```
|
392
|
-
|
393
|
-
By using the `after_commit` callback we can account for this case.
|
394
|
-
|
395
|
-
```ruby
|
396
|
-
class PictureFile < ActiveRecord::Base
|
397
|
-
after_commit :delete_picture_file_from_disk, on: [:destroy]
|
398
|
-
|
399
|
-
def delete_picture_file_from_disk
|
400
|
-
if File.exist?(filepath)
|
401
|
-
File.delete(filepath)
|
402
|
-
end
|
403
|
-
end
|
404
|
-
end
|
405
|
-
```
|
406
|
-
|
407
|
-
NOTE: the `:on` option specifies when a callback will be fired. If you
|
408
|
-
don't supply the `:on` option the callback will fire for every action.
|
409
|
-
|
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.
|