Permalink
Browse files

Merge pull request #13829 from qsymmachus/improve_validates_with_example

Reordered classes in AR Validation #validates_with example [ci skip]
  • Loading branch information...
2 parents 0a43bf3 + 098a960 commit c438c61794f78d2b2f9023de707c84f9ee5e652f @rafaelfranca rafaelfranca committed Jan 24, 2014
Showing with 8 additions and 8 deletions.
  1. +8 −8 guides/source/active_record_validations.md
@@ -616,17 +616,17 @@ 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"
record.errors[:base] << "This person is evil"
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,17 +644,17 @@ 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" }
record.errors[:base] << "This person is evil"
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

0 comments on commit c438c61

Please sign in to comment.