dimanche 9 août 2015

Error messages for child added in before_save callback is not appearing in parent model

I have two models Parent and child

Parent
   has_many :child
   accepts_nested_attributes_for :child

Child
   belongs_to :parent

Both Parent and Child has an attribute email in it. I wanted to do a custom validation for email of both Parent and Child. So I added a custom validation which is common to both in before_save callback in their models respectively and also I am including error messages to the model in that custom validation once the validation gets failed. The problem is once the child does not pass the validation, the error messages for the child is not appearing in parent model (@parent.errors) but the error attribute is there in messages hash of @parent.errors.

But when I included that custom validation in before_validation callback of both models.The error message of Child appeared in Parent model. Why is it so?

Aucun commentaire:

Enregistrer un commentaire