Given the relationship expressed below:
class Parent < ActiveRecord::Base
has_many :children, :dependent => :destroy
accepts_nested_attributes_for :child
end
class Child < ActiveRecord::Base
belongs_to :parent
validates :name, :presence => true
end
Let’s assume we have a parent object with multiple children, one or more of which have errors that cause parent.valid? to return false.
parent = Parent.new
parent.build_child(:name => "steve")
parent.build_child()
parent.valid?
Is there a way to access the child element that caused the errors when looking at the parent.errors object?
As John suggested in the comments, I ended up ignoring the errors added to the parent for the children and traversing the children and adding their errors manually. The issue was complicated by having a couple has_many :through relationships, but John’s suggestion was the essence of what I ended up using.