我仍然试图了解如何在Ecto中处理创建/更新has_many, through:
关联的问题。我已经重新阅读José's关于关联的帖子以及the docs,但我仍在努力。
我拥有的是:
网络/模型/ dish.ex
defmodule Mp.Dish do
use Mp.Web, :model
schema "dishes" do
# ...
has_many :dish_dietary_prefs, Mp.DishDietaryPref, on_delete: :delete_all,
on_replace: :delete
has_many :dietary_prefs, through: [:dish_dietary_prefs, :dietary_pref]
end
# ...
end
网络/模型/ dietary_pref.ex
defmodule Mp.DietaryPref do
use Mp.Web, :model
schema "dietary_prefs" do
# ...
has_many :dish_dietary_prefs, Mp.DishDietaryPref, on_delete: :delete_all,
on_replace: :delete
has_many :dishes, through: [:dish_dietary_prefs, :dish]
end
# ...
end
网络/模型/ dish_dietary_pref.ex
defmodule Mp.DishDietaryPref do
use Ecto.Schema
schema "dish_dietary_prefs" do
belongs_to :dish, Mp.Dish
belongs_to :dietary_pref, Mp.DietaryPref
end
end
我有一个JSON端点接收Dish
的参数,其中我有一个名为dietary_prefs
的密钥,它以逗号分隔的字符串形式传递,例如:
[info] POST /api/vendors/4/dishes
[debug] Processing by Mp.Api.DishController.create/2
Parameters: %{"dish" => %{"dietary_prefs" => "2,1"}, "vendor_id" => "4"}
(此SO帖子删除了"dish"
的其他参数。)
如何在控制器中处理此问题?具体来说,我想要这种行为:
POST
个请求(创建操作),请在dish_dietary_prefs
中创建必要的记录,以将此新Dish
与给定的DietaryPref
相关联。逗号分隔的字符串是id
个DietaryPref
个记录。PUT/PATCH
请求(更新),在dish_dietary_prefs
中创建/销毁必要的记录以更新关联(用户可以将菜肴重新分配给不同的饮食主题)。DELETE
个请求,请销毁dish_dietary_prefs
。我认为已经使用模型中的on_delete
配置处理了这种情况。我的控制器中已经有了为给定供应商创建/更新菜肴的逻辑(这只是一个简单的has_many/belongs_to
关系),但我仍然无法弄清楚如何创建/更新/为特定的菜肴破坏这些联想。
非常感谢任何帮助。
如果我"need to receive the IDs and manually build the intermediate association for each" DietaryPref
我正在关联Dish
,我可以举例说明如何在控制器中执行上述规范吗?
更新 :刚看到Ecto 2.0.0-beta.1已经发布,那就是supports many_to_many
,这看起来像是一个解决方案我的问题。任何人都有一个在行动中使用它的例子,就像我上面描述的那样?
答案 0 :(得分:8)
感谢无畏的绝地大师何塞·瓦利姆本人,我已经弄明白了(在Ecto 2.0.0-beta.1
中):
这是我的最终控制者:
def create(conn, %{"dish" => dish_params }, vendor) do
dietary_prefs = get_dietary_pref_changeset(dish_params["dietary_prefs"])
changeset = vendor
|> build_assoc(:dishes)
|> Repo.preload(:dietary_prefs)
|> Dish.changeset(dish_params)
|> Ecto.Changeset.put_assoc(:dietary_prefs, dietary_prefs)
case Repo.insert(changeset) do
{:ok, dish} ->
conn
|> put_status(:created)
|> render("show.json", dish: dish)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(ChangesetView, "error.json", changeset: changeset)
end
end
def update(conn, %{"id" => id, "dish" => dish_params}, vendor) do
dish = Repo.get!(vendor_dishes(vendor), id)
dietary_prefs = get_dietary_pref_changeset(dish_params["dietary_prefs"])
changeset = dish
|> Repo.preload(:dietary_prefs)
|> Dish.changeset(dish_params)
|> Ecto.Changeset.put_assoc(:dietary_prefs, dietary_prefs)
case Repo.update(changeset) do
{ :ok, dish } ->
render(conn, "show.json", dish: dish)
{ :error, changeset } ->
conn
|> put_status(:unprocessable_entity)
|> render(ChangesetView, "error.json", changeset: changeset)
end
end
defp vendor_dishes(vendor) do
assoc(vendor, :dishes)
end
defp parse_dietary_pref_ids(ids) do
ids
|> String.split(",")
|> Enum.map(fn(x) -> Integer.parse(x) |> Kernel.elem(0) end)
end
defp get_dietary_prefs_with_ids(ids) do
from(dp in DietaryPref, where: dp.id in ^ids) |> Repo.all
end
defp get_dietary_pref_changeset(param) do
param
|> parse_dietary_pref_ids
|> get_dietary_prefs_with_ids
|> Enum.map(&Ecto.Changeset.change/1)
end
https://groups.google.com/forum/#!topic/elixir-ecto/3cAi6nrsawk