I have a model with a couple of accepts_nested_attributes_for. There is a requirement that I have at least one of each of the nested attributes when saving and moving on.
However, validation occurs pre-save, so when I’m removing an item and moving on, it let’s it through.
How can I validate that when I’ve saved, I have at least one item of each nested type there?
There’s a bug with accepts_nested_attributes_for. Meaning you have to be a little more devious when it comes to validations in the parent model.
You could use an :after_save callback in each of your nested models to check if it’s the last one. But if there’s many nested associations where you want to ensure at least one, this isn’t very DRY.
This is however a valid workaround for the linked bug: