How to create nested objects using accepts_nested_attributes_for
I’ve upgraded to Rails 2.3.3 (from 2.1.x) and I’m trying to figure out the
If I try to create a new
It fails validations with the message:
I understand why this is happening — it’s because of the
I could also solve the problem by manually creating the
Is there a standard Rails way of creating nested objects on new records?
This is a common, circular dependency issue. There is an existing LightHouse ticket which is worth checking out.
I expect this to be much improved in Rails 3, but in the meantime you’ll have to do a workaround. One solution is to set up a virtual attribute which you set when nesting to make the validation conditional.
And then you would set this attribute as a hidden field in your form.
That should be enough to have the