ActiveRecord Ranges 18 Jun 2011
Just a quick one today, I’m going to mention a quick trick you may have heard about, but is definitely worth knowing.
When using ActiveRecord as well as passing a String/Integer or Array into a query you can also use a Range.
I find this particularly helpful when searching by date.
For example instead of:
You can use a range, e.g.
Notice how using an inclusive range produces a SQL BETWEEN query.
Using an exclusive range gives a different query.
Use carefully.
For an interesting use of this check out one of the new methods added to Rails 3.1 check out Date.today.all_day added here.
Tweet