Named Scope Extensions - Calling Method Outside Do Block
I have the following named_scope in my User model:
named_scope :all_stars, :joins => [:all_stars] do
def overall
se开发者_C百科lf.find(:all, :conditions => ['recordable_type = ?', 'User'])
end
end
I want to do this:
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
Can it be done?
If you can make the other thing into another named scope, you can then chain together the two scopes, which will get you what you want.
named_scope :all_stars, :joins => [:all_stars]
named_scope :overall, :conditions => ['recordable_type = ?', 'User']
Then you should be able to call it as such:
object.all_stars.overall.all
object.overall.all_stars.find(:all)
# etc
And also create a method that does the same thing:
def overall_all_stars_condition
self.all_stars.overall.all
end
精彩评论