aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-01-25 09:10:35 -0200
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-01-25 16:36:27 -0200
commit6a85c45b1cced856da3377fe1eb50b1f8436cb9e (patch)
tree87f0a1ce5f55d97d22549f5fd6c640e24be8733d /railties/guides
parent42159354b4ee6145fe3fa9e169af48380c6134e0 (diff)
downloadrails-6a85c45b1cced856da3377fe1eb50b1f8436cb9e.tar.gz
rails-6a85c45b1cced856da3377fe1eb50b1f8436cb9e.tar.bz2
rails-6a85c45b1cced856da3377fe1eb50b1f8436cb9e.zip
Fix custom validation methods section in AR validations and callbacks guide
The methods validate_on_create and validate_on_update are not available anymore in Rails, this removes them from the guide and adds an example on how to use validate with the :on option.
Diffstat (limited to 'railties/guides')
-rw-r--r--railties/guides/source/active_record_validations_callbacks.textile16
1 files changed, 14 insertions, 2 deletions
diff --git a/railties/guides/source/active_record_validations_callbacks.textile b/railties/guides/source/active_record_validations_callbacks.textile
index a27c292a4c..ac5ee0694c 100644
--- a/railties/guides/source/active_record_validations_callbacks.textile
+++ b/railties/guides/source/active_record_validations_callbacks.textile
@@ -614,7 +614,7 @@ As shown in the example, you can also combine standard validations with your own
h4. Custom Methods
-You can also create methods that verify the state of your models and add messages to the +errors+ collection when they are invalid. You must then register these methods by using one or more of the +validate+, +validate_on_create+ or +validate_on_update+ class methods, passing in the symbols for the validation methods' names.
+You can also create methods that verify the state of your models and add messages to the +errors+ collection when they are invalid. You must then register these methods by using the +validate+ class method, passing in the symbols for the validation methods' names.
You can pass more than one symbol for each class method and the respective validations will be run in the same order as they were registered.
@@ -637,12 +637,24 @@ class Invoice < ActiveRecord::Base
end
</ruby>
+By default such validations will run every time you call +valid?+. It is also possible to control when to run these custom validations by giving an +:on+ option to the +validate+ method, with either: +:create+ or +:update+.
+
+<ruby>
+class Invoice < ActiveRecord::Base
+ validate :active_customer, :on => :create
+
+ def active_customer
+ errors.add(:customer_id, "is not active") unless customer.active?
+ end
+end
+</ruby>
+
You can even create your own validation helpers and reuse them in several different models. For example, an application that manages surveys may find it useful to express that a certain field corresponds to a set of choices:
<ruby>
ActiveRecord::Base.class_eval do
def self.validates_as_choice(attr_name, n, options={})
- validates attr_name, :inclusion => { {:in => 1..n}.merge(options) }
+ validates attr_name, :inclusion => { { :in => 1..n }.merge!(options) }
end
end
</ruby>