I have a Sinatra app with a Project
model and a Task
model, and a project has_many
tasks.
It's a composition type of relationship, meaning a project cannot exist without tasks associated.
I create a project on /projects/new
, then redirect to /projects/:id/tasks/new
to add tasks to the project. On the latter page, I:
- instantiate the newly-created project instance from the param in the URL
- create a number of tasks
- validate all the tasks
- add them to the project.
The problem is, if one gets interrupted during any of the step above, then no tasks will be added and the project will be saved in the database with no tasks associated. This will result in errors when I calculate total task duration, and in other situations.
I could instantiate and save both records on the same page, and that would solve the problem.
But, is there a way to split Project
and Task
creation across dedicated URLs without this resulting in childless projects?