2011-04-12 58 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