diff options
author | Angelo capilleri <capilleri@yahoo.com> | 2013-03-13 14:11:46 +0100 |
---|---|---|
committer | Angelo capilleri <capilleri@yahoo.com> | 2013-03-13 14:15:15 +0100 |
commit | 8b3483f0a8144d0093108acc28d50814076dcf73 (patch) | |
tree | 268a4a363cb7139d53bbdf7e5ce586dcf3c94c39 /guides | |
parent | 008fd54e74aa01e590074861fd2e6d82bed82af8 (diff) | |
download | rails-8b3483f0a8144d0093108acc28d50814076dcf73.tar.gz rails-8b3483f0a8144d0093108acc28d50814076dcf73.tar.bz2 rails-8b3483f0a8144d0093108acc28d50814076dcf73.zip |
absence validator doc
Diffstat (limited to 'guides')
-rw-r--r-- | guides/source/active_record_validations.md | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/guides/source/active_record_validations.md b/guides/source/active_record_validations.md index 32641d04c1..7dbe464f34 100644 --- a/guides/source/active_record_validations.md +++ b/guides/source/active_record_validations.md @@ -530,6 +530,47 @@ field you should use `validates :field_name, inclusion: { in: [true, false] }`. The default error message is _"can't be empty"_. +### `absence` + +This helper validates that the specified attributes are absent. It uses the +`present?` method to check if the value is not either nil or a blank string, that +is, a string that is either empty or consists of whitespace. + +```ruby +class Person < ActiveRecord::Base + validates :name, :login, :email, absence: true +end +``` + +If you want to be sure that an association is absent, you'll need to test +whether the associated object itself is absent, and not the foreign key used +to map the association. + +```ruby +class LineItem < ActiveRecord::Base + belongs_to :order + validates :order, absence: true +end +``` + +In order to validate associated records whose absence is required, you must +specify the `:inverse_of` option for the association: + +```ruby +class Order < ActiveRecord::Base + has_many :line_items, inverse_of: :order +end +``` + +If you validate the absence of an object associated via a `has_one` or +`has_many` relationship, it will check that the object is neither `present?` nor +`marked_for_destruction?`. + +Since `false.present?` is false, if you want to validate the presence of a boolean +field you should use `validates :field_name, inclusion: { in: [true, false] }`. + +The default error message is _"must be blank"_. + ### `uniqueness` This helper validates that the attribute's value is unique right before the |