I have a Rails association between models Project
and Queue
. A project has_many queues. A queue must have a project, and consequently has a presence validation on project_id
Suppose I want to create a new project WITH queues. For example, something like this:
project = Project.new(valid: param, options: here)
project.queues << Queue.new(other_valid: param, options: here)
project.save!
The save is going to fail because the queues fail the project_id presence validation.
My usual ugly way of getting around this is to create a project, then add queues, and wrap the whole lot in a transaction so that if any part of the process fails, it rolls back. ...Somehow that seems uglier than it should be.
So, is there a more graceful way of creating queues on a new project without hitting the presence validation, but still assert that those queues must have a project?
Cheers