문제

How should I write 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

Mohit Jain's answer adapted for 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)

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top