使用 ecto 2.0 更新多条记录

Update multiple records using ecto 2.0

我可以在 ecto 2.0 中插入多条记录:

iex(1)> categories = [%{name: "Whosebug", url: "whosebug.com"}]

iex(2)> App.Repo.insert_all App.Category, categories

是否可以一次更新多条记录?

iex(1) > category = App.Repo.all(App.Category) |> hd

iex(2) > changeset = App.Category.changeset(category, %{name: "test"})

iex(3)> App.Repo.insert_all App.Category, [changeset]

我有一个场景,我必须每天在抓取外部页面后插入或更新类别。似乎 Ecto 2.0 中有 Multi 功能。关于如何解决这个问题的任何指示?

就像有一个insert_all/3 function, there is also an update_all/3函数一样。 update_all/3 采用可查询(%MyApp.SomeModel{} 或查询)、更新的关键字列表([name: "John"])和可选的选项关键字列表。