命名范围扩展 - 在Do Block之外调用方法

我的User模型中有以下named_scope:
  named_scope :all_stars, :joins => [:all_stars] do
    def overall
      self.find(:all, :conditions => ['recordable_type = ?', 'User'])
    end
  end
我想做这个:
  named_scope :all_stars, :joins => [:all_stars] do
    def overall
      overall_all_stars_condition
    end
  end

  def overall_all_stars_condition
    self.find(:all, :conditions => ['recordable_type = ?', 'User']) 
  end
可以吗?     
已邀请:
如果你可以将另一个东西放到另一个命名范围中,那么你可以将两个范围链接在一起,这样可以得到你想要的东西。
named_scope :all_stars, :joins => [:all_stars]
named_scope :overall, :conditions => ['recordable_type = ?', 'User']
然后你应该能够这样称呼它:
object.all_stars.overall.all
object.overall.all_stars.find(:all)
# etc
并且还创建了一个执行相同操作的方法:
def overall_all_stars_condition
  self.all_stars.overall.all
end
    

要回复问题请先登录注册