使用with_scope扩展查询

2015-11-29  本文已影响22人  Kjiang

上一篇我们讲到将常用的find查询写入model中,但是那样写还存在一个问题,就是我们在外部调用的时候不支持扩展,比如:

@tasks = Task.find_incomplete(limit: 20)

下面我们给出通过with_scope的方式使方法接受这样的扩展查询:

def self.find_incomplete options={}
  with_scope :find => options do 
    find_all_by_complete(false, order: 'created_at DESC')
  end
end

于2015-03-20

上一篇 下一篇

猜你喜欢

热点阅读