I am trying to created a nested form with a HABTM association and I am unable to find an approach that works for all cases.
What I am trying to achieve is a Project form that can:
1. Create a new project and create new company to associate with it
- This works perfectly
2. Create a new project with an existing company via autocomplete.
- This fails because it's looking for an existing record in the HABTM join table *before* there is a project id.
Here is the SQL that comes out of this failed case:
SELECT "companies".* FROM "companies" INNER JOIN "companies_projects" ON "companies".id = "companies_projects".company_id WHERE ("companies"."id" IN ('6')) AND ("companies_projects".project_id = NULL )
In my Project model I am using: accepts_nested_attributes_for :companies
Is this the wrong approach to tackling this use case? I've read that this is only good for 1-1 or 1-n relationships. What is the preferred method for doing something like this?
Thanks in advance, David