Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
May 3 ’12

Using ActiveRecord to query for times within a range

You can pass a range to query for records within that range:

hackerinspiration:

Just discovered this, something I wish I knew a LONG time ago.

Album.where(:created_at => 2.days.ago..Time.now)

Which will generate the following SQL query (depending on the database):

SELECT "albums".* FROM "albums" WHERE ("albums"."created_at" BETWEEN '2012-04-28 11:10:22.780712' AND '2012-04-30 11:10:22.780907')

16 notes 0 comments (via hackerinspiration-blog)

  1. stuartjohnston reblogged this from hackerinspiration-blog
  2. nuna-blog1 reblogged this from rubyquicktips
  3. rubyquicktips reblogged this from hackerinspiration-blog and added:
    You can pass a range to query for records within that range:
  4. hackerinspiration-blog posted this

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus