Hello everyone,
I absolutely hit a roadblock and might need some help.
I have two schemas that are in a many-to-many relationship to each other. Here is a trimmed down version of both schemas:
Subtasks:
schema "subtasks" do
field :description, :string
field :status, :string
field :title, :string
many_to_many :tasks, SomeProject.Task.Task, join_through: "task_subtasks"
timestamps()
end
Tasks:
schema "tasks" do
field :title, :string
field :start_date, :string
field :end_date, :string
many_to_many :subtasks, SomeProject.Task.SubTask, join_through: "task_subtasks"
timestamps()
end
Now I try to write a test driven create method.
My input data (that I get from a Phoenix form) looks like this:
@valid_create_attrs %{
title: "Task 1",
start_date: "01.01.2020",
end_date: "01.05.2020",
subtasks: [
%{
title: "Exercise 1",
description: "Some Text 1",
status: "open"
},
%{
title: "Exercise 2",
description: "Some Text 2",
status: "finished"
}
]
}
So I know that I have to send the struct through a changeset and also create a association via build_assoc but I’m absolutely not sure how this method might look like.
Any help is appreciated!
Edit: I’m tinkering around in the iex shell because I think a possible solution might be to perform pattern matching on the map. So I basically retrieve everything except the tasks entry to create the task changeset. Then I extract the tasks list and create one changeset for each item. Unfortunately I’m currently not able to perform the pattern matching…