开发者

Named Scope Extensions - Calling Method Outside Do Block

开发者 https://www.devze.com 2023-02-23 14:45 出处:网络
I have the following named_scope in my User model: named_scope :all_stars, :joins => [:all_stars] do

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
0

精彩评论

暂无评论...
验证码 换一张
取 消