2013-03-19 71 views
1

我想通過我的截止時間屬性創建一個範圍,可以在我的模型中找到例如less_than_a_year,less_than_six_month的數據集。Rails 3.2模型範圍日期less_than_a_

class Goal < ActiveRecord::Base 
    attr_accessible :category, :title, :detail, :deadline, :achieve 

    #need help solving this 
    scope :less_than_a_year, where() 
end 

所以它會執行goal.less_than_a_year並提供一年以下的數據列表。 謝謝。

回答

2

根據rails 3.2 guides

scope :less_than_a_year, lambda { where("deadline < ?", 1.year.from_now) } 
0

我很確定這是數據庫特定的,但它應該適用於大多數。

class Goal < ActiveRecord::Base 
    scope :less_than_a_year, lambda{ where("deadline < ?", 1.year.from_now) } 
end 

這假定的最後期限是DateDateTime

編輯:添加拉姆達(我發誓,我這樣做之前,我看到其他的答案,但不是downvote前)

0
class Goal < ActiveRecord::Base 

#need help solving this 
scope :less_than_a_year, labmda { where("deadline < ", 1.year.ago)} 

end 
+3

你忘了?和拼寫錯誤的lambda – mraaroncruz 2013-03-19 14:54:13