schema "user" do
field :name, :string
has_many(:user_company_assocs, UserCompanyAssoc)
has_many(:companies, through: [:user_company_assocs, :company])
end
And a model user_company_assoc:
schema "user_company" do
field :deleted, :boolean
belongs_to(:company, Company)
belongs_to(:user, User)
end
I want to insert_all with the association, like this:
But when it reaches to the second insert_all, I lose the company_ids because it is not recorded in the user table. The user names are saved and I have an array of users with the name and ids, but since the company_ids is just used on the generate_assoc_changesets, it looses the conection with each user. I still have access to the full payload with the company_ids, but don’t have the new users ids to create the right associations.
I read in the docs that insert_all has no support for associations, but I think it is only automatic assocs with put_assoc on the changeset. In my case I’m trying to manually create the associations with multi operations. So it shouldn’t be a limitation.
As far as I understood Multi.run and Multi.insert_all, both have access to the previous previous operations. Like this: