How to get record created today by rails activerecord?
Ruby on-RailsDateRails ActiverecordRuby on-Rails Problem Overview
How should I write the conditional statement for when I want to get all the records which were created today?
Ruby on-Rails Solutions
Solution 1 - Ruby on-Rails
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
Solution 2 - Ruby on-Rails
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
Solution 3 - Ruby on-Rails
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
Solution 4 - Ruby on-Rails
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)
Solution 5 - Ruby on-Rails
Mohit Jain's answer adapted for Rails3
Model.where "DATE(created_at) = DATE(?)", Time.now
Solution 6 - Ruby on-Rails
This "namescopes" the attribute with the table_name
.
Solution 7 - Ruby on-Rails
model.rb
scope :posted_today, -> { posted_between_period(Time.now.midnight, Time.now.end_of_day) }
posts_controller.rb
Post.posted_today
Solution 8 - Ruby on-Rails
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'")
Solution 9 - Ruby on-Rails
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
Solution 10 - Ruby on-Rails
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.