我有
class Foo < ActiveRecord::Base named_scope :a, lambda { |a| :conditions => { :a => a } } named_scope :b, lambda { |b| :conditions => { :b => b } } end
我想要
class Foo < ActiveRecord::Base named_scope :ab, lambda { |a,b| :conditions => { :a => a, :b => b } } end
但我宁愿以干燥的方式做这件事.我可以通过使用获得相同的效果
Foo.a(something).b(something_else)
但它并不是特别可爱.
至少从3.2开始就有一个聪明的解决方案:
scope :optional, ->() {where(option: true)} scope :accepted, ->() {where(accepted: true)} scope :optional_and_accepted, ->() { self.optional.merge(self.accepted) }