Rails 中 scope

来源:互联网 发布:洛阳中国联合网络通信 编辑:程序博客网 时间:2024/06/06 18:31

Rails 中自由地使用命名 scope

# 可以链式调用class User < ActiveRecord::Base  scope :old, -> { where('age > 60') }  scope :heavy, -> { where('weight > 200') }end#链式调用可以执行 User.old.heavy
# 不能链式调用class User < ActiveRecord::Base  def User.old    where('age > ?', 80)  end  def User.heavy    where('weight > ?', 200)  endend#这种方式下 old 和 heavy 可以单独工作,但不能执行 User.old.heavy