0
我在我的模型上有一個自定義方法,我想在where條件中使用它,我已經使用自定義方法對此進行了排序,但無法找到用於搜索的方法自定義條件。Rails-自定義模型方法的條件
現在我有這個,在我的模型
attr_accessible :end, :start
def time_left
self.end.to_i - Time.now.utc.to_i - Time.now.utc_offset
end
def time_left_to_start
self.start.to_i - Time.now.utc.to_i - Time.now.utc_offset
end
def status
if self.time_left_to_start > 0
1 #proximamente
elsif self.time_left_to_start < 0 and self.time_left > 0
2 #funcionando
else
3 #finalizado
end
end
和我的控制器
def index
@next_events = Event.all.sort_by(&:time_left).reverse!.reject { |e| e.status != 1 }.take(5)
end
我知道我可以寫在那裏方法的整個狀態,但我的想法是不重複我的自我,並避免循環所有的事件,以刪除狀態不是1的每一個,我只需要每次最後5個,並且看起來對我來說真的是一個糟糕的主意,每次向所有記錄請求db。 在此先感謝,問候!
做這種事情是不可能的。我想你需要編寫一段SQL代碼。 –