首页 > 技术文章 > 4种scope方法

tardis 2015-02-03 16:12 原文

默认作用域,自动加载:

default_scope { order(created_at: :desc) }

 

model 调用 find_2时才运行

scope :find_2, ->{ where('clients.id' => 2 ) }

 

下面都需要传参,使用model.find_2(id)调用

scope :find_2, ->(id) { where('clients.id' => id) }

scope :find_2, lambda { |id| where('clients.id' => id) }

推荐阅读