1

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 (...).

Megamind
  • 111
  • 1
  • 7
  • possible duplicate of [Rails ActiveRecord date between](http://stackoverflow.com/questions/2381718/rails-activerecord-date-between) – xlembouras Oct 18 '14 at 17:41
  • Lower limit <= Value and upper limit > value, The operators are not like lower_limit < value and upper_limit > value or lower_limit <= value and upper_limit >= value – Megamind Oct 18 '14 at 17:47
  • What are `lower_limit` and `upper_limit`? Are the columns? – mu is too short Oct 18 '14 at 17:52
  • Yes, you can look at this question for whole picture: http://stackoverflow.com/questions/26442782/chaining-of-conditional-where-clauses-using-rails-active-record – Megamind Oct 18 '14 at 17:56
  • Why don't you update this question instead of asking a new one? You're allowed to clarify your questions through editing. – mu is too short Oct 18 '14 at 18:12
  • That is a different question. Read completely. – Megamind Oct 18 '14 at 18:19

2 Answers2

1
Model.where(:count => lower_limit..upper_limit)
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
eshaiju
  • 753
  • 5
  • 16
  • Have you bothered to look at the SQL this produces? Do you know what BETWEEN does in SQL? Did notice that the question talks about `lower_limit` and `upper_limit` rather than a single column? – mu is too short Oct 18 '14 at 17:54
0

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)
jibai31
  • 1,663
  • 18
  • 22