Ecto.Query “where [column] in (list of values)”

I have a list of states ie [AL, NY, OH] and I want match each value in the list to the field in a where clause.

I’ve tried:

|> Query.where(s.state = String.contains(states))

|> Query.where(Enum.member(s.state, states))

but both of these return a is not a valid query expression error.
Does documentation exist for the Query.[expression] syntax? Everything in the ecto documentation I’ve seen structures it in the standard postgres syntax.

2 Likes

Expression that can be used in queries are documented in Ecto.Query.API module, see entry for in/2 which will allow you to do s.state in ^states.

8 Likes

I like how I unknowingly answered my own question in the title…

11 Likes

Just means you were going on the right path! :slight_smile:

3 Likes

I love when the answer turns out to be very intuitive. That happens more often than not for me in Elixir.

2 Likes