I have Users, Projects and Tasks.
Users have many tasks
Projects have tasks
Tasks belongs to users
Tasks belong to projects
One of the fields of the Tasks Model is Project_id.
I need that when a user creates a new task, a new project is created, and the task created gets the id of the new project created in its Project_id field, so it gets automatically associated with it.
My problem is that when creating the new task, the project has not been created yet, so the project_id is null, could anyone please at least point me in the right direction?
How about doing it in a
before_createcallback?The
return falsebit there is necessary to prevent theTaskobject from saving with an invalidProjectand to add errors to it that can be displayed in your view.