1

締め切り属性を使用して、モデル内のデータセット(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を実行し、1年未満のデータのリストを提供します。ありがとうございました。

4

3 に答える 3

2

Rails3.2ガイドによると

scope :less_than_a_year, lambda { where("deadline < ?", 1.year.from_now ) }
于 2013-03-19T14:52:37.033 に答える
0

これはデータベース固有のものであると確信していますが、ほとんどの場合は機能するはずです。

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

これは、期限がまたはであると想定していDateますDateTime

編集:ラムダを追加しました(他の回答を見る前にこれを行ったことを誓いますが、反対票の前ではありません)

于 2013-03-19T14:49:39.583 に答える
0
class Goal < ActiveRecord::Base

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

end
于 2013-03-19T14:52:50.363 に答える