如何将现有的named_scope添加到匿名范围?

时间:2010-09-17 10:29:31

标签: ruby-on-rails activerecord named-scope

我有一个模特:

class Shirt < ActiveRecord::Base
  named_scope :red, :conditions => { :color => 'red' }
  named_scope :blue, :conditions => { :color => 'blue' }
  named_scope :xl,  :conditions => { :size  => 'xl'  }
end

我忘了,如何轻松地将命名范围添加到现有的匿名范围:

scope = Shirt.scoped({})
#and how to add ie. :red to scope?

2 个答案:

答案 0 :(得分:1)

Shirt.scoped({}).red

答案 1 :(得分:0)

这可以通过以下代码实现:

named_scope :colour, lambda { |colour_id| {:conditions => ["colour_id = ?", colour_id])}}

您可以链接命名范围:

Shirt.red.xl