Many to many relationship

Sure no problem @venomnert,

you would need a changeset function for the customerlead_service schema as well.

def changeset(customerlead_service, attrs) do
  customerlead_service
  |> cast(attrs, [:customer_leads_id, :service_categories_id])
  |> validate_required([:customer_leads_id, :service_categories_id])
end

To create the record you would need the parent tables to be created already


customer_lead = %CustomerLead{...}
service_category = %ServiceCategory{...}
attrs = %{"customer_leads_id" => customerlead.id, "service_categories_id" => service_category.id}

and call this function (which should be in your Context) passing attrs

def create_customerlead_service(attrs = %{}) do
  %CustomerLeadService{}
  |> CustomerLeadService.changeset(attrs)
  |> Repo.insert()
end

Not relevant to the question but I would change field names in the customerlead_service schema from :customer_leads and :service_categories to :customer_lead and service_category as you’re only referring to one and not several in the intermediary table.