Module: ActiveModel::Validations::HelperMethods
- Defined in:
- lib/nested_validator/nested_validator.rb
Instance Method Summary collapse
-
#validates_nested(*attr_names) ⇒ Object
Bases an object’s validity on nested attributes.
Instance Method Details
#validates_nested(*attr_names) ⇒ Object
Bases an object’s validity on nested attributes.
class Parent < ActiveRecord::Base
has_one :child
validates_nested :child
end
class Child < ActiveRecord::Base
attr_accessor :attribute
validates :attribute, presence: true
validates_presence_of :attribute
end
Any errors in the child will be copied to the parent using the child’s name as a prefix for the error:
puts parent.errors. #=> { :'child attribute' => ["can't be blank"] }
133 134 135 |
# File 'lib/nested_validator/nested_validator.rb', line 133 def validates_nested(*attr_names) validates_with NestedValidator, _merge_attributes(attr_names) end |