128

How should I write the conditional statement for when I want to get all the records which were created today?

10 Answers 10

255
Post.where(created_at: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day)

PS: This answer has been modified as answer by Harish Shetty was better than mine. As my answer is accepted one. I have updated this answer for community support

1
  • 8
    In the latest versions this can be even more streamlined with Post.where(created_at: Time.current.all_day)
    – Eyeslandic
    Feb 2, 2021 at 9:06
130

I know this question has an accepted answer. The solution suggested in the accepted answer can cause performance issues when the table size grows.

Typically, if you perform lookups based on created_at column, add an index on the table in your migration file.

add_index :posts, :created_at

Now, to lookup records created today:

Rails 3/4

Post.where("created_at >= ?", Time.zone.now.beginning_of_day)

To lookup posts created on a specific day.

Post.where(:created_at => (date.beginning_of_day..date.end_of_day))

--------- OR -------------

Add a static method to your model

class Post < ActiveRecord::Base
  def self.today
    where("created_at >= ?", Time.zone.now.beginning_of_day)
  end
end

Post.today #returns posts today

Rails 2

Post.all(:conditions => ["created_at >= ?", Time.zone.now.beginning_of_day])

--------- OR -------------

Add a named_scope to your model

class Post < ActiveRecord::Base    
  named_scope :today, lambda { 
    {
      :conditions => ["created_at >= ?", Time.zone.now.beginning_of_day]
    }
  }
end

Post.today #returns posts today
3
  • 1
    Great point about indexing. Just wanted to clarify that the scope example in this post is for Rails 3 only, since it looks like it's under the Rails 2 heading. In Rails 2, you would need to use named_scope rather than scope. Also, in Rails 3, you could equivalently use a class method def self.today where("created_at >= ?", Time.now.beginning_of_day) end which is probably cleaner than using a scope in this case, since it allows you to forgo the lambda. Jan 31, 2013 at 18:56
  • @evanrmurphy can/should you revise your comment from "Rails 3 only" to "Rails 3 and greater"?
    – Kai Vong
    Feb 11, 2014 at 6:10
  • @kaichanvong I'm not familiar with Rails 4, so I would like to say "Rails 3 (and greater?)" but SO won't let me edit the comment :-/ Feb 11, 2014 at 23:22
35

Rails 5.1 has an all_day helper that's useful here.

Post.where(created_at: Date.today.all_day)

or

Post.where(created_at: Date.parse("YYYY-MM-DD").all_day)
0
31

MySQL:

Model.all :condition => ["DATE(created_at) = ?", Date.today] # rails 2
Model.where("DATE(created_at) = ?", Date.today) # rails 3

PostgreSQL:

Model.all :condition => ["created_at::date = ?", Date.today] # rails 2
Model.where("created_at::date = ?", Date.today) # rails 3
0
20

Mohit Jain's answer adapted for Rails3

Model.where "DATE(created_at) = DATE(?)", Time.now
9

Post.where(created_at: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day)

This "namescopes" the attribute with the table_name.

5

model.rb

scope :posted_today, -> { posted_between_period(Time.now.midnight, Time.now.end_of_day) }

posts_controller.rb

Post.posted_today
1
  • 3
    @Pathiv, between_period looks interesting. I didn't find any documentation for it. Can you provide some link? How does rails choose the column for comparison? Nov 16, 2014 at 3:32
1

For some reason, none of the other solutions in this post nor others on StackOverflow worked for me (using Rails 4.2.4 and Ruby 2.2.3p173). This is the only query that I could get to work with my Postgres database:

Post.where("created_at >= TIMESTAMP 'now'")
-1

To query records which created from today

Use scope with arel

class Post < ActiveRecord::Base    
  scope :create_from_today, -> {
    where(arel_table[:created_at].gteq(Time.zone.now.beginning_of_day))
  }
end

Then we can use it

today_posts = Post.created_from_today
3
  • where('created_at >= now()') would only find items where the created_at was in the future. Apr 24, 2019 at 13:27
  • Yeah I would remove it from the answer, nice catch thanks
    – Hieu Pham
    Apr 25, 2019 at 15:40
  • The issue you now have is that records that are marked with future dates would be presented as well as those for today. If you're trying to avoid using between, you should specify .lteq(Time.zone.now.end_of_day)) as well. May 16, 2019 at 9:47
-5

In rails 4.2.3 for getting the records created today, using mysql use the following.

@usergoals = Goal.where("userid = :userid and Date(created_at) = :date", { userid: params[:id], date: Date.today })

here i am using multiple conditions if you want you can edit it for single condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.