How can I make a many to many relation with ecto 2? As an example app I want to
create a Post which can be in multiple categories. The categories already exist. For example:
[%Category{id: "1", name: "elixir"}, %Category{id: "2", name: "erlang"}]
Im using Ecto 2 beta 0. The example project is called Ecto2.
I defined two models:
defmodule Ecto2.Post do
use Ecto2.Web, :model
use Ecto.Schema
schema "posts" do
field :title, :string
many_to_many :categories, Ecto2.Category, join_through: "posts_categories", on_replace: :delete
timestamps
end
@required_fields ~w(title)
@optional_fields ~w()
def changeset(model, params \ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
|> cast_assoc(:categories) # not suitable?
end
end
defmodule Ecto2.Category do
use Ecto2.Web, :model
schema "categories" do
field :name, :string
timestamps
end
@required_fields ~w(name)
@optional_fields ~w()
def changeset(model, params \ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
end
end
I tried doing it like this:
post = Repo.get!(Post, 1) |> Repo.preload(:categories)
changeset = Post.changeset(post, %{"title"=> "bla", "categories"=> [%{id: "1"}]})
Repo.update!(changeset)
But cast_assoc in Post.changeset is not suitable for this task, it wants to create a whole new Category instead of associate one.
What should I use instead? build_assoc? But build_assoc docs do not mention it is useful with many_to_many. How do I use it? Should I put build_assoc in Post.changeset then, or should i use it in a phoenix controller.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…