diff options
Diffstat (limited to 'guides/source/active_record_validations.md')
-rw-r--r-- | guides/source/active_record_validations.md | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/guides/source/active_record_validations.md b/guides/source/active_record_validations.md index 67cc6a4db3..31c5b07a05 100644 --- a/guides/source/active_record_validations.md +++ b/guides/source/active_record_validations.md @@ -231,9 +231,9 @@ Errors](#working-with-validation-errors) section. ### `errors.details` -To check what validator type was used on invalid attribute, you can use -`errors.details[:attribute]`. It returns array of hashes where under `:error` - key you will find symbol of used validator. +To check which validations failed on an invalid attribute, you can use +`errors.details[:attribute]`. It returns an array of hashes with an `:error` +key to get the symbol of the validator: ```ruby class Person < ActiveRecord::Base @@ -245,7 +245,7 @@ end >> person.errors.details[:name] #=> [{error: :blank}] ``` -Using `details` with custom validators are covered in the [Working with +Using `details` with custom validators is covered in the [Working with Validation Errors](#working-with-validation-errors) section. Validation Helpers @@ -606,9 +606,7 @@ This helper validates that the attribute's value is unique right before the object gets saved. It does not create a uniqueness constraint in the database, so it may happen that two different database connections create two records with the same value for a column that you intend to be unique. To avoid that, -you must create a unique index on both columns in your database. See -[the MySQL manual](http://dev.mysql.com/doc/refman/5.6/en/multiple-column-indexes.html) -for more details about multiple column indexes. +you must create a unique index on that column in your database. ```ruby class Account < ActiveRecord::Base @@ -628,6 +626,7 @@ class Holiday < ActiveRecord::Base message: "should happen once per year" } end ``` +Should you wish to create a database constraint to prevent possible violations of a uniqueness validation using the `:scope` option, you must create a unique index on both columns in your database. See [the MySQL manual](http://dev.mysql.com/doc/refman/5.6/en/multiple-column-indexes.html) for more details about multiple column indexes or [the PostgreSQL manual](http://www.postgresql.org/docs/9.4/static/ddl-constraints.html) for examples of unique constraints that refer to a group of columns. There is also a `:case_sensitive` option that you can use to define whether the uniqueness constraint will be case sensitive or not. This option defaults to @@ -1079,7 +1078,7 @@ Another way to do this is using `[]=` setter ```ruby class Person < ActiveRecord::Base def a_method_used_for_validation_purposes - errors[:name] = "cannot contain the characters !@#%*()_-+=" + errors.messages[:name] << "cannot contain the characters !@#%*()_-+=" end end @@ -1094,39 +1093,40 @@ Another way to do this is using `[]=` setter ### `errors.details` -You can add validator type to details hash when using `errors.add` method. +You can specify a validator type to the returned error details hash using the +`errors.add` method. ```ruby - class Person < ActiveRecord::Base - def a_method_used_for_validation_purposes - errors.add(:name, :invalid_characters) - end +class Person < ActiveRecord::Base + def a_method_used_for_validation_purposes + errors.add(:name, :invalid_characters) end +end - person = Person.create(name: "!@#") +person = Person.create(name: "!@#") - person.errors.details[:name] - # => [{error: :invalid_characters}] +person.errors.details[:name] +# => [{error: :invalid_characters}] ``` -To improve error details to contain not allowed characters set, you can -pass additional options to `errors.add` method. +To improve the error details to contain the unallowed characters set for instance, +you can pass additional keys to `errors.add`. ```ruby - class Person < ActiveRecord::Base - def a_method_used_for_validation_purposes - errors.add(:name, :invalid_characters, not_allowed: "!@#%*()_-+=") - end +class Person < ActiveRecord::Base + def a_method_used_for_validation_purposes + errors.add(:name, :invalid_characters, not_allowed: "!@#%*()_-+=") end +end - person = Person.create(name: "!@#") +person = Person.create(name: "!@#") - person.errors.details[:name] - # => [{error: :invalid_characters, not_allowed: "!@#%*()_-+="}] +person.errors.details[:name] +# => [{error: :invalid_characters, not_allowed: "!@#%*()_-+="}] ``` -All built in Rails validators populate details hash with corresponding -validator types. +All built in Rails validators populate the details hash with the corresponding +validator type. ### `errors[:base]` |