2013-07-22 5 views
0

레코드 집합을 반환하는 메서드를 구현 한 모델이 있습니다. Arel에서 그들을 참조 할 수 있습니까?Arel에서 사용자 정의 ActiveRecord 메서드를 혼합/참조/호출하는 방법

class A < ActiveRecord::Base 
    #associations here 

    def self.mymeth 
    #return a set of records based on a query 
    B.select(col).joins(:cs).where(some_condition) 
    end 

end 

class B < ActiveRecord::Base 
    #associations here 
end 

class C < ActiveRecord::Base 
    #associations here  
end 

지금 내가 어떻게 당신이 scopes 찾고 있지

A.joins(:mymeth).where(condition).count 

답변

1

처럼 뭔가 mymeth 있습니까 참조 할 수 있습니다?

class A < ActiveRecord::Base 

    scope :myscope, lambda { joins(:b).where(column: true) } 
end 

당신은 다음과 범위를 호출 할 수라고 할 때 범위에 추가됩니다

A.mymeth.where(col: false) 

모든 SQL 조건은 자동으로 쿼리에 추가됩니다.