How to build WHERE IN array clause with Ecto?

luzny picture luzny · Apr 23, 2016 · Viewed 16.4k times · Source

How to find posts in given List of ids?

This isn't working:

posts = Post |> where(id: [1, 2]) |> Repo.all

Example in Rails:

Post.where({ id: [1, 2]})
# SELECT * FROM posts WHERE id IN (1, 2)

Answer

Gazler picture Gazler · Apr 23, 2016

The following should work:

posts = Post |> where([p], p.id in [1, 2]) |> Repo.all