Ruby-on-rails 如何通过 rails activerecord 获取今天创建的记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2919720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get record created today by rails activerecord?
提问by Victor Lam
How should I write the conditional statement for when I want to get all the records which were created today?
当我想获取今天创建的所有记录时,我应该如何编写条件语句?
回答by Mohit Jain
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
PS:这个答案已被修改,因为 Harish Shetty 的答案比我的好。因为我的回答被接受了。我已更新此答案以获得社区支持
回答by Harish Shetty
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_atcolumn, add an index on the table in your migration file.
通常,如果您根据created_at列执行查找,请在迁移文件中的表上添加索引。
add_index :posts, :created_at
Now, to lookup records created today:
现在,要查找今天创建的记录:
Rails 3/4
导轨 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
导轨 2
Post.all(:conditions => ["created_at >= ?", Time.zone.now.beginning_of_day])
--------- OR-------------
--------- 或-------------
Add a named_scope to your model
将 named_scope 添加到您的模型
class Post < ActiveRecord::Base
named_scope :today, lambda {
{
:conditions => ["created_at >= ?", Time.zone.now.beginning_of_day]
}
}
end
Post.today #returns posts today
回答by jigfox
MySQL:
MySQL:
Model.all :condition => ["DATE(created_at) = ?", Date.today] # rails 2
Model.where("DATE(created_at) = ?", Date.today) # rails 3
PostgreSQL:
PostgreSQL:
Model.all :condition => ["created_at::date = ?", Date.today] # rails 2
Model.where("created_at::date = ?", Date.today) # rails 3
回答by thekindofme
Mohit Jain's answer adapted for Rails3
Mohit Jain 的答案适用于 Rails3
Model.where "DATE(created_at) = DATE(?)", Time.now
回答by StevenClontz
Rails 5.1 has an all_dayhelper that's useful here.
Rails 5.1 有一个all_day在这里很有用的助手。
Post.where(created_at: Date.today.all_day)
or
或者
Post.where(created_at: Date.parse("YYYY-MM-DD").all_day)
回答by Rafael Oliveira
Post.where(created_at: Time.zone.now.beginning_of_day..Time.zone.now.end_of_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.
这“名称范围”具有table_name.
回答by Parthiv
model.rb
模型.rb
scope :posted_today, -> { posted_between_period(Time.now.midnight, Time.now.end_of_day) }
posts_controller.rb
post_controller.rb
Post.posted_today
回答by Tim S
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:
出于某种原因,本文中的其他解决方案和 StackOverflow 上的其他解决方案都不适用于我(使用 Rails 4.2.4 和 Ruby 2.2.3p173)。这是我可以使用 Postgres 数据库的唯一查询:
Post.where("created_at >= TIMESTAMP 'now'")
回答by Hieu Pham
To query records which created from today
查询从今天开始创建的记录
Use scope with arel
将作用域与 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
回答by santu
In rails 4.2.3 for getting the records created today, using mysql use the following.
在 rails 4.2.3 中获取今天创建的记录,使用 mysql 使用以下内容。
@usergoals = Goal.where("userid = :userid and Date(created_at) = :date", { userid: params[:id], date: Date.today })
@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.
如果您愿意,我在这里使用多个条件,您可以针对单个条件对其进行编辑。

