我正在尝试创建一个范围,可以通过我的截止日期属性在我的模型中找到数据集,例如less_than_a_year,less_than_six_month。
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并提供少于一年的数据列表。 谢谢。
答案 0 :(得分:2)
scope :less_than_a_year, lambda { where("deadline < ?", 1.year.from_now ) }
答案 1 :(得分:0)
我很确定这是特定于数据库的,但它应该适用于大多数。
class Goal < ActiveRecord::Base
scope :less_than_a_year, lambda{ where("deadline < ?", 1.year.from_now) }
end
这假定截止日期为Date
或DateTime
。
编辑:添加了lambda(我发誓我在看到其他答案之前做过这个,但不是在downvote之前)
答案 2 :(得分:0)
class Goal < ActiveRecord::Base
#need help solving this
scope :less_than_a_year, labmda { where("deadline < ", 1.year.ago)}
end