aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source/active_record_validations.md
diff options
context:
space:
mode:
authorJohn Olmsted & Strand McCutchen <pair+qsymmachus.strand@devbootcamp.com>2014-01-24 11:24:06 -0800
committerJohn Olmsted & Strand McCutchen <pair+qsymmachus.strand@devbootcamp.com>2014-01-24 11:30:07 -0800
commit098a960266dbcabaa6169940520f1f4cf2169cdf (patch)
treefb112cab0a8605c28c50384376ebbf6be8ad6c59 /guides/source/active_record_validations.md
parentf5c3531ade409f98328e8e05ed5a0d9a8514d308 (diff)
downloadrails-098a960266dbcabaa6169940520f1f4cf2169cdf.tar.gz
rails-098a960266dbcabaa6169940520f1f4cf2169cdf.tar.bz2
rails-098a960266dbcabaa6169940520f1f4cf2169cdf.zip
Reordered classes in AR Validation #validates_with example [ci skip]
Person called GoodnessValidator before it was defined. This change will compile the example correctly.
Diffstat (limited to 'guides/source/active_record_validations.md')
-rw-r--r--guides/source/active_record_validations.md16
1 files changed, 8 insertions, 8 deletions
diff --git a/guides/source/active_record_validations.md b/guides/source/active_record_validations.md
index efa826e8df..b04c7a90e2 100644
--- a/guides/source/active_record_validations.md
+++ b/guides/source/active_record_validations.md
@@ -616,10 +616,6 @@ The default error message is _"has already been taken"_.
This helper passes the record to a separate class for validation.
```ruby
-class Person < ActiveRecord::Base
- validates_with GoodnessValidator
-end
-
class GoodnessValidator < ActiveModel::Validator
def validate(record)
if record.first_name == "Evil"
@@ -627,6 +623,10 @@ class GoodnessValidator < ActiveModel::Validator
end
end
end
+
+class Person < ActiveRecord::Base
+ validates_with GoodnessValidator
+end
```
NOTE: Errors added to `record.errors[:base]` relate to the state of the record
@@ -644,10 +644,6 @@ Like all other validations, `validates_with` takes the `:if`, `:unless` and
validator class as `options`:
```ruby
-class Person < ActiveRecord::Base
- validates_with GoodnessValidator, fields: [:first_name, :last_name]
-end
-
class GoodnessValidator < ActiveModel::Validator
def validate(record)
if options[:fields].any?{|field| record.send(field) == "Evil" }
@@ -655,6 +651,10 @@ class GoodnessValidator < ActiveModel::Validator
end
end
end
+
+class Person < ActiveRecord::Base
+ validates_with GoodnessValidator, fields: [:first_name, :last_name]
+end
```
Note that the validator will be initialized *only once* for the whole application