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)
end
end
#这种方式下 old 和 heavy 可以单独工作,但不能执行 User.old.heavy