Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get record created today by rails activerecord?

People also ask

What does ActiveRecord base mean?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.

What is ActiveRecord in Ruby on Rails?

What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.

What is ActiveRecord relation?

Whereas an instance of ActiveRecord::Relation is a representation of a query that can be run against your database (but wasn't run yet). Once you run that query by calling to_a , each , first etc. on that Relation a single instance or an array of ActiveRecord::Base instances will be returned.

What is an ActiveRecord object?

In Active Record, objects carry both persistent data and behavior which operates on that data. Active Record takes the opinion that ensuring data access logic as part of the object will educate users of that object on how to write to and read from the database.


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


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

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

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)

Mohit Jain's answer adapted for Rails3

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

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

This "namescopes" the attribute with the table_name.


model.rb

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

posts_controller.rb

Post.posted_today