class Parent
has_one :child
accepts_nested_attributes_for :child
end
class Child
belongs_to :parent
end
Using a nested object form, I need to add some extra validations to the child model. These are not always run on Child, so I cannot put them in a validate method in Child. It seems sensible to do the checking in a validate method in Parent, but I'm having trouble adding error messages correctly.
This does work:
class Parent
...
def validate
errors[ :"child.fieldname" ] = "Don't be blank!"
end
But we lose nice things like I18n and CSS highlighting on the error field.
This doesn't work:
def validate
errors.add :"child.fieldname", :blank
end