开发者

How to get record created today by rails activerecord?

开发者 https://www.devze.com 2023-01-01 00:54 出处:网络
How should I write开发者_JAVA技巧 the conditional statement for when I want to get all the records which were created today?Post.where(created_at: Time.zone.now.beginning_of_day..Time.zone.now.end_of_

How should I write开发者_JAVA技巧 the conditional statement for when I want to get all the records which were created today?


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


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'")


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消