gpt4 book ai didi

ruby-on-rails - 本周的范围日期属性?

转载 作者:太空宇宙 更新时间:2023-11-03 17:58:53 26 4
gpt4 key购买 nike

我正在尝试确定本周所有产品的范围,因此它应该显示一周中任何一天之前的所有产品。

class Product < ActiveRecord::Base
attr_accessible :purchase_date

def self.this_weeks
where("purchase_date >= ?", Date.at_beginning_of_week - Date.at_end_of_week)
end

create_table :products do |t|
t.date :purchase_date
end
end

虽然这给了我一个错误:

undefined method `at_beginning_of_week'

我需要纠正什么?

最佳答案

at_beginning_of_week 已在 Rails 3 中删除。您应该使用 beginning_of_week但是,请注意,这是一个实例方法。所以你必须做类似的事情:

Date.today.beginning_of_week

此外,您可以使用范围并使您的查询非常易于阅读:

where(:purchase_date => Date.today.beginning_of_week..Date.today.end_of_week)

关于ruby-on-rails - 本周的范围日期属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8505997/

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