1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Reordered classes in AR Validation #validates_with example [ci skip]

Person called GoodnessValidator before it was defined.
This change will compile the example correctly.
This commit is contained in:
John Olmsted & Strand McCutchen 2014-01-24 11:24:06 -08:00
parent f5c3531ade
commit 098a960266

View file

@ -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