Is there a way to do validations across models within the nested structure of a nested model form? In the nested hierarchy I’m working with, a child model references an attribute in the parent to perform a validation. Since the validations are done from the bottom up, (the child model is validated first), the child does not have a reference to the parent and the validation fails. For example:
# encoding: utf-8
class Child < ActiveRecord::Base
attr_accessible :child_attribute
belongs_to :parent
validate :to_some_parent_value
def to_some_parent_value
if child_attribute > parent.parent_attribute # raises NoMethodError here on ‘parent’
errors[:child_attribute] << "Validation error."
end
end
end
class Parent < ActiveRecord::Base
attr_accessible :parent_attribute
has_one :child
accepts_nested_attributes_for :child
end
In console:
> p=Parent.new( { "parent_attribute" => "1", "child_attributes" => { "child_attribute" => "2" }} )
> p.valid?
=> NoMethodError: undefined method `parent_attribute' for nil:NilClass
Is there a way to have this kind of validation where the child references a value in the parent and still use the Rails nested model forms feature?