diff options
author | Santiago Pastorino <santiago@wyeworks.com> | 2012-09-24 07:52:50 -0700 |
---|---|---|
committer | Santiago Pastorino <santiago@wyeworks.com> | 2012-09-24 07:52:50 -0700 |
commit | 1dbe4baef5120dce845c46abe0014abf64e4b0ca (patch) | |
tree | ec70465bc628036fa7147195bf843b63dd4af1ee /guides | |
parent | dae474e876b54c2d9a9e778210ae387901f31afc (diff) | |
parent | f31ea4df3ac760ab7ff18ea439e9a9ce9b8c625a (diff) | |
download | rails-1dbe4baef5120dce845c46abe0014abf64e4b0ca.tar.gz rails-1dbe4baef5120dce845c46abe0014abf64e4b0ca.tar.bz2 rails-1dbe4baef5120dce845c46abe0014abf64e4b0ca.zip |
Merge pull request #6936 from jfoley/callbacks
Fix collisions with before and after validation callbacks.
Diffstat (limited to 'guides')
-rw-r--r-- | guides/source/active_record_validations_callbacks.md | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/guides/source/active_record_validations_callbacks.md b/guides/source/active_record_validations_callbacks.md index e5957d8acb..f32c1050ce 100644 --- a/guides/source/active_record_validations_callbacks.md +++ b/guides/source/active_record_validations_callbacks.md @@ -995,6 +995,25 @@ class User < ActiveRecord::Base end ``` +Callbacks can also be registered to only fire on certain lifecycle events: +<ruby> +class User < ActiveRecord::Base + before_validation :normalize_name, :on => :create + + # :on takes an array as well + after_validation :set_location, :on => [ :create, :update ] + + protected + def normalize_name + self.name = self.name.downcase.titleize + end + + def set_location + self.location = LocationService.query(self) + end +end +</ruby> + 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. Available Callbacks |