如何在实现软删除的情况下使用Ecto.Repo的预加载功能

时间:2018-01-24 19:31:17

标签: elixir phoenix-framework ecto

我可以通过执行以下操作来查询软件删除的,关联的数据库条目:

git reset --hard 23456787654

这与一些深层嵌套的关联变得乏味。如何使用Ecto.Repo的内置预加载函数来查询适用于所有关联的where子句?

我想做点什么:

query = from r in Resource,
        join a in Association, on: [resource_id: r.id]
        where: is_nil(a.deleted_at)

Repo.all(query)

这样的事情是可能的,还是我可以用另一种方法来实现同样的目标?

1 个答案:

答案 0 :(得分:1)

一种方法是,你应该能够做到这样的事情:

def list_resources() do
  from(r in Resource, preload: [foo: ^not_deleted(Foo), bar: ^not_deleted(Bar)])
  |> Repo.all()
end

def not_deleted(query) do
  from(q in query, where: is_nil(q.deleted_at))
end
相关问题