There’s no need to give up, things aren’t THAT bad! ![]()
You will always get better results with questions about Ecto if you are specific about what you are trying to do. In particular, this is important:
This specific task is alternatively phrased as “I want to insert an employee and also create a role associated to an existing organization”, which then translates to Ecto operations:
organization = get_the_org_somehow()
role = Ecto.build_assoc(organization, :roles, %{"name" => "owner", etc})
# role is unsaved and has an organization_id but no employee_id
employee_changeset =
%Employee{}
|> Employee.registration_changeset(attrs)
|> Ecto.Changeset.put_assoc(:roles, [role])
# changeset contains an unsaved Employee with one unsaved Role
result = Repo.insert(employee_changeset)
You might also refactor this to make Employee.registration_changeset take an Organization and do the put_assoc there, to keep things tidy.






















