gpt4 book ai didi

ruby-on-rails - Rails 3 : How can I find rows in a table that were created today?

转载 作者:行者123 更新时间:2023-12-03 16:01:13 25 4
gpt4 key购买 nike

最好我想使用这样的语句......
current_user.workouts.find_by_created_at(Time.now.day)
但这不起作用,我猜是因为时间不匹配。我将继续阅读文档,但我想我会在阅读时发布这个问题以寻求帮助。

谢谢!

更新
使用新的 Rails 3 对 ARel 和命名范围的支持,我将查询重构为...

模型

   class Workout < ActiveRecord::Base
scope :from_today, where(" created_at between ? AND ?", Time.zone.now.beginning_of_day, Time.zone.now.end_of_day)
.
.
.
end

Controller
def create
workouts = current_user.workouts.from_today
.
.
.
end

最佳答案

current_user.workouts.find(:all, :conditions => [" YEAR(created_at) = ? AND MONTH(created_at) = ? AND DAY(created_at) = ?", Time.zone.now.year, Time.zone.now.month, Time.zone.now.day])

或者
current_user.workouts.find(:all, :conditions => [" created_at between ? AND ?", Time.zone.now.beginning_of_day, Time.zone.now.end_of_day])

不确定哪个更优化

关于ruby-on-rails - Rails 3 : How can I find rows in a table that were created today?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5559846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com