简体   繁体   中英

How to get record created today by rails activerecord?

当我想要获取今天创建的所有记录时,我应该如何编写条件语句?

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

Mohit Jain的答案适用于Rails3

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

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)

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

这个“使用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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM