hawkyre

hawkyre

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?

Showing Posts 1 to 7

al2o3cr

al2o3cr

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.

hawkyre

hawkyre OP

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.

al2o3cr

al2o3cr

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.

hawkyre

hawkyre OP

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.

hawkyre

hawkyre OP

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

dimitarvp

dimitarvp

Did you manage to make this work eventually?

hawkyre

hawkyre OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews