Ecto build_assoc同时具有多个模型

时间:2016-12-29 07:38:06

标签: elixir ecto

Ecto.build_assoc/3中,是否可以将模型列表作为第3个参数传递?假设我的产品型号与类别模型有很多关系,而我想与此产品关联的3个现有类别,是否可以执行以下操作:

product = Repo.get!(Product, 1)
#assume the below function exists and returns a list of categories
categories = Category.get_children(:fashion)

#here, the docs say the third parameter can be a map/keyword list of attributes, but thats ok if you are associating a single model. In my case, I have many so am trying to pass a list of maps(structs for that matter)
assocs = Ecto.build_assoc(product, :categories, categories)

以上代码可以工作吗?

0 个答案:

没有答案