本周的范围日期属性? [英] Scoping date attribute for this week?

查看:41
本文介绍了本周的范围日期属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to scope all of my Products for this week, so it should show all the products leading up to whichever day of the week.

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

虽然这给了我一个错误:

This gives me an error though:

undefined method `at_beginning_of_week'

我需要纠正什么?

推荐答案

at_beginning_of_week 已在 Rails 3 中删除.您应该使用 beginning_of_week 但是,请注意,这是一个实例方法.因此,您必须执行以下操作:

at_beginning_of_week has been removed in Rails 3. You should use beginning_of_week but, be careful, it's an instance method. So you have to do something like:

Date.today.beginning_of_week

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

Furthermore, you can use a range and make your query very nice to read:

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

这篇关于本周的范围日期属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆