E
E
endenwer2015-10-25 11:11:03
Ruby on Rails
endenwer, 2015-10-25 11:11:03

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_fordoes not work, because when creating Children, the parent is not created yet, inverse_ofnot working with polymorphic

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
N. Bekseitov, 2015-10-26
@nbekseitov

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 question

Ask a Question

731 491 924 answers to any question