Ash FrameworkAF
Ash Framework3y ago
26 replies
victorbjorklund

Additional attributes on join table for many-to-many relationship

I have an app that has courses and teachers. The relationship is many to many since each course can have many teachers and each teacher can have many courses. In the join table/resource I have an additional attribute called role (one can be primary or extra teacher for a course). I managed to connect the resources so that I can create a new relationship by calling:

TheSchoolApp.Courses.Teacher.update(teacher, %{courses: [%{id: course.id, role: "primary"}]})

The problem is that it doesn't fill in the role attribute. It just leaves it as nil.

The attributes and relationship for CourseTeacher is:

attributes do

uuid_primary_key(:id)


attribute(:role, :string, allow_nil?: true)

create_timestamp(:inserted_at)
end

relationships do
belongs_to :teacher, TheSchoolApp.Courses.Teacher, primary_key?: true, allow_nil?: false
belongs_to :course, TheSchoolApp.Courses.Course, primary_key?: true, allow_nil?: false
end

And the action for teacher is:

update :update do

argument :courses, {:array, :map} do
allow_nil? false
end

change manage_relationship(:courses, type: :append_and_remove)
end
Was this page helpful?