Is there a preferred way to create a hierarchy of new ActiveRecord model objects with associations (e.g. creating a model that has_many children) within a single action? Is this just something that should be done in separate bits?
Take the example of a blog post model which has_many comments. I add support for the author of the blog post adding an initial comment within the same form for the blog post. Right now, what I do is have an after_create call in the blog post that checks to see if there is a comment, and the blog post creates a comment if it exists.
I was thinking of just building (.build) the comment with an unsaved blog post, but apparently that does not work since the blog post does not actually have an id yet since it has not yet been saved. I’m interested in finding out what approaches other people have taken.
My preference is for nested model forms.
Models:
Controller:
View: