Bradley Priest

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:

  Widget.where('created_at > ? AND created_at < ?', 2.hours.ago,
    #=> SELECT "widgets".* FROM "widgets" WHERE (created_at > '2011-06-18 03:38:58.493361' AND created_at < '2011-06-18 05:38:58.493442')

You can use a range, e.g.

  Widget.where(:created_at =>
    #=> SELECT "widgets".* FROM "widgets" WHERE ("widgets"."created_at" BETWEEN '2011-06-18 03:36:53.551349' AND '2011-06-18 05:36:53.551489')

Notice how using an inclusive range produces a SQL BETWEEN query.

Using an exclusive range gives a different query.

  Widget.where(:created_at =>
    #=> SELECT "widgets".* FROM "widgets" WHERE ("widgets"."created_at" >= '2011-06-11 05:25:12.738961' AND "widgets"."created_at" < '2011-06-18 05:25:12.739321')

Use carefully.

For an interesting use of this check out one of the new methods added to Rails 3.1 check out added here.

blog comments powered by Disqus