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 attr_names.
Instance Method Details
#validates_nested(*attr_names) ⇒ Object
Bases an object’s validity on nested attr_names.
class Parent < ActiveRecord::Base
has_one :child
validates_nested :child
end
class Child < ActiveRecord::Base
has_one :child
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"] }
113 114 115 |
# File 'lib/nested_validator/nested_validator.rb', line 113 def validates_nested(*attr_names) validates_with NestedValidator, _merge_attributes(attr_names) end |