Answer the question
In order to leave comments, you need to log in
How to validate in RoR that the Parrent model has at least one Children, and the validations of the Children model require a Parrent?
There are models Parrent1 and Parrent2 c has_many :children
, which must have at least one Children. And there is a Children model with belongs_to :parrentable, polymorphic: true
. However, the validation of Children requires some fields of the parent model. How can this be implemented in RoR?
I can create all this in one transaction: First, the parent model is saved (it has no validation on Children), then Children are created (they have access to the parent, because it is already saved), then it is checked that the parent has at least one child. If the check fails, then RollBack is called and nothing is saved. Everything works, but I would like to know if there is a built-in functionality for this in RoR. accepts_nested_attributes_for
does not work, because when creating Children, the parent is not created yet, inverse_of
not working with polymorphic
Answer the question
In order to leave comments, you need to log in
Should a child have at least one parent?
For non-polymorphic relationships, you can use this article:
Using belongs_to to handle multiple Parent Records...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question