aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Foley <john@thefol.io>2012-09-23 12:49:34 -0600
committerJohn Foley <john@thefol.io>2012-09-23 12:57:19 -0600
commitf31ea4df3ac760ab7ff18ea439e9a9ce9b8c625a (patch)
tree215ceca36da5efa52e800aba2bd8f62b1696b432
parent60c65ca8dfc197b83f3b74b7e6ddeede005d416b (diff)
downloadrails-f31ea4df3ac760ab7ff18ea439e9a9ce9b8c625a.tar.gz
rails-f31ea4df3ac760ab7ff18ea439e9a9ce9b8c625a.tar.bz2
rails-f31ea4df3ac760ab7ff18ea439e9a9ce9b8c625a.zip
Add CHANGELOG entry and update the guide
-rw-r--r--activerecord/CHANGELOG.md4
-rw-r--r--guides/source/active_record_validations_callbacks.md19
2 files changed, 23 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 04b67cdf3a..344ee6416d 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ##
+* Allow before and after validations to take an array of lifecycle events
+
+ *John Foley*
+
* Support for specifying transaction isolation level
If your database supports setting the isolation level for a transaction, you can set
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