将Date和1.month.ago放在一起?

Lea*_*RoR 5 ruby ruby-on-rails ruby-on-rails-3

我怎么会把Date1.month.ago在一起的时候我已经叫日期属性:purchase_date,并希望把它的一类方法中?

def self.last_month # Show only products of last month.
  where(:purchase_date => Date.today.1.month.ago.end_of_month..Date.1.month.ago.beginning_of_month)
end
Run Code Online (Sandbox Code Playgroud)

控制台给出语法错误并将其删除Date.today,与我的其他方法相比,给出了空白结果:

def self.this_month # Show only products of this month.
   where(:purchase_date => Date.today.beginning_of_month..Date.today.end_of_month)
end
Run Code Online (Sandbox Code Playgroud)

Mar*_*oda 11

仅仅1.month.ago是不够的,你并不需要预先考虑Date.today1.month.ago,因为1.month.ago从今天开始


Mik*_*kin 7

你的日期语法有错误,你可能想要使用这样的东西:

def self.last_month # Show only products of last month.
  where(:purchase_date => 1.month.ago.beginning_of_month..1.month.ago.end_of_month)
end

def self.this_month # Show only products of this month.
   where(:purchase_date => Date.today.beginning_of_month..Date.today.end_of_month)
end
Run Code Online (Sandbox Code Playgroud)