Fill in `has_many` relationship while creating a new record

As it says in the title, I am trying to fill in a has_many relationship while creating a new record, but everything that I’ve been able to find requires the record to already exist before filling in the has_many.

Here’s the setup that I have:

schema "course" do
  has_many(:tiers, CourseTiers)
end

schema "tier" do
  has_many(:courses, CourseTiers)
end

schema "course_tiers" do
  belongs_to(:course, Course)
  belongs_to(:tier, Tier)
  field(:order, :integer)
end

The problem comes whenever I try to create a course with the reference to the tiers in the same changeset as the new course. Since neither the course_tiers nor the course exist yet, I haven’t been able to use either build_assoc, cast_assoc, or put_assoc effectively.

Is there any workaround to this that does not involve using multi or performing the insert operation in multiple steps?

cast_assoc should 100% be able to construct this shape - but your associations are declared strangely.

For instance, has_many(:tiers, CourseTiers) says that you want an association named tiers on Course that has CourseTiers structs in it. That’s… not probably what you want.

A more-standard set of associations:

schema "course" do
  has_many(:course_tiers, CourseTiers)
  has_many(:tiers, Tier, through: :course_tiers)
end

schema "tier" do
  has_many(:course_tiers, CourseTiers)
  has_many(:courses, Course, through: :course_tiers)
end

# join table schema same as before

Then the changeset for a Course can cast_assoc :course_tiers, and CourseTiers.changeset can cast_assoc :tiers.


General aside: Ecto doesn’t care much about pluralization, but human readers might. Naming tables with singular forms (course, tier) and schemas with plural ones (CourseTiers) feels odd IMO.

The CourseTiers naming thing was just an issue because I did that example without copypasting but it is indeed CourseTier in my code.

Alright, I tried renaming the association to :course_tiers but it still gives me an error. This is part of the changeset error, the one which shows the invalid changeset (and just one of the elements):

course_tier_groups: [
       #Ecto.Changeset<
         action: :insert,
         changes: %{order: 0, tier_group_id: 14},
         errors: [course_id: {"can't be blank", [validation: :required]}],
         data: #LLServer.Schema.CourseTierGroup<>,
         valid?: false
       >,

I’ll re-post the structure as it is right now (instead of tiers I have tier groups, it was just easier to write it down but it’s the same thing):

Course schema:

schema "course" do
...
  has_many(:course_tier_groups, CourseTierGroup, on_replace: :delete)
...
end

Course changeset:

def changeset(%Course{} = struct, params \\ %{}) do
  struct
  |> Repo.preload([:scopes, :type, :course_tier_groups])
  |> cast(params, [:title, :description, :tag, :type_metadata])
  |> put_scopes
  |> put_type
  |> cast_assoc(:course_tier_groups, with: &CourseTierGroup.changeset/2)
  |> validate_required([:title, :description, :tag])
  |> unique_constraint([:tag])
end

CourseTierGroup schema:

schema "course_tier_group" do
  belongs_to(:course, Course)
  belongs_to(:tier_group, TierGroup)
  field(:order, :integer)
end

TierGroup schema:

schema "tier_group" do
...
  has_many(:tiers, TierInTierGroup, on_replace: :delete)
  # Won't post more, irrelevant, but this just keeps going a bit
...
end

What I pass to the changeset on create:
[%{"tier_group_id" => X, "order" => Y}, ...]

On update it works but only if I manually put the course_id before calling the API.

Then when that is passed the course_id property of course_tier_group doesn’t get filled and it throws an error since the changeset is invalid.

Don’t use validate_required to verify associations; see also the docs:

Do not use this function to validate associations that are required, instead pass the :required option to cast_assoc/3 or cast_embed/3.

1 Like

As I understand it, I have to use cast_assoc in CourseTierGroup in order for it to work, right? The thing is, if I do so, I would have to pass the whole tier_group object through the API since cast_assoc expects a whole object to do an update operation in case IDs match. Is that correct? I do not want to perform such update operation, I just want to set the IDs of the respective props in CourseTierGroup.

I’ve been trying to change up the format but I still can’t get this to work :confused:

Did you manage to make this work eventually?

Yes, I ended up splitting creation and updating in 2 different flows:

  • In create, I run a multi where I first create the course then I create the associations with the course’s new ID
  • In update, I just fill in the course_id and run it through the changeset normally

It’s so much more complicated than I expected it to be, but it does the job.