2011-04-12 55 views
0

我在用户模式下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 

能不能做到?

回答

2

如果您可以将另一个事物放入另一个命名范围,那么您可以将这两个范围链接在一起,这会得到您想要的。

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