2011-12-15 4 views
4

:purchase_dateと呼ばれる日付属性があり、クラスメソッド内に配置したい場合は、どのようにしてDate1.month.agoを一緒に入れますか?日付と1.month.agoをまとめる?

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 

コンソールには、構文エラーを与え、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 
+3

私はちょうど '1.month.ago'で十分でなければならないと思っています。あなたはDate.today、' 1.month.ago'は今日から始まります。 – maprihoda

+0

@maprihoda、正しいです... go私たちはあなたをupvoteすることができますので、それを答えてください:) –

+0

@マーク完了、ありがとう! – maprihoda

答えて

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 
+0

Heh、私はそれを逃した、ありがとう。 – LearningRoR

+0

それはまだ魅力のように動作します! – Abhinay

8

だけ1.month.agoが十分である、あなたは1.month.agoためにDate.todayを付加する必要はありません。今日から開始

関連する問題