foundart
foundart

Reputation: 370

Ecto query where testA and (testB or testC)

I have a schema that looks like this;

schema "things" do
  field(:title, :string)
  field(:temperature, :integer)
  field(:weight, :integer)
end

I'd like to write an Ecto query that is equivalent to the following SQL, but I can't seem to get it into a valid form.

select * from things where temperature <= 200 and (weight is null or weight > 2);

How can I do this in Ecto?

Upvotes: 2

Views: 59

Answers (1)

Dogbert
Dogbert

Reputation: 222388

It's almost the same syntax as SQL:

from(t in Thing, where: t.temperature <= 200 and (is_nil(t.weight) or t.weight > 2))

Upvotes: 2

Related Questions