Megamind
Megamind

Reputation: 111

Rails way of Where Between

I need to check for the following condition:

lower_limit <= VALUE < upper_limit

I can do the above using

Model.where("lower_limit <= ? AND upper_limit > ?", value, value) 

but how do I do the same using ranges ? (..)/(...)

Please note the operators:

The first operator of between is "<=" and second operator is ">". Had they been same, I could have used (..) or (...).

Upvotes: 1

Views: 132

Answers (2)

jibai31
jibai31

Reputation: 1885

You cannot do that with a range in the query, but you can still make use of BETWEEN. In MySQL, BETWEEN is inclusive. You could write something like:

Model.where("? BETWEEN lower_limit AND upper_limit - 1", value)

Which would generate SQL like:

SELECT * FROM models WHERE (37 BETWEEN lower_limit AND upper_limit - 1)

Upvotes: 0

eshaiju
eshaiju

Reputation: 763

Model.where(:count => lower_limit..upper_limit)

Upvotes: 1

Related Questions