Ruby on rails 本周的作用域日期属性?

Ruby on rails 本周的作用域日期属性?,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我正在努力确定本周所有产品的范围,因此它应该显示一周中任何一天之前的所有产品 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

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

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

我需要更正什么?

在周初,Rails 3中已删除了周初。您应该使用,但要小心,它是一个实例方法。所以你必须做一些类似的事情:

Date.today.beginning_of_week
此外,您可以使用一个范围,使您的查询非常易于阅读:

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

在开始时,在Rails 3中删除了周的。您应该使用,但要小心,它是一个实例方法。所以你必须做一些类似的事情:

Date.today.beginning_of_week
此外,您可以使用一个范围,使您的查询非常易于阅读:

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

谢谢你的两个答案,我将使用新的可读方式。谢谢你的两个答案,我将使用新的可读方式。