2012-09-14 2 views
0

여러 입력 매개 변수를 기반으로 복잡한 범위를 작성하여 특정 특성과 일치하는 제품과 연관된 개체를 Relationship 개체로 반환하는 데 사용하는 클래스 메서드 Relationship.with_attributes이 있습니다. 대부분의 경우, 나는에 대해 일치하는 정보가 Product 모델이며, 모두가 잘 :조건을 누적하는 동안 여러 테이블 조인

내 속성이 범주 인 경우 문제는 내가 Categorizations 모델에 참여하는 방법을 결정하지 못하고있다
def self.with_attributes(attributes) 
    if (attributes.nil?) 
    scoped 
    else # (attributes.class == Hash) 
    conds = joins(:product) 

    attributes.each do |key, value| 
     case key 
     when "Category" 
     # FIXME This doesn't work yet 
     category = Category.find(value) 
     categories = [category] + category.descendants 
     categories.each { |c| conds.push(["categories.id = #{c.id}"], :or) } 
     else 
     conds.where(key => value) 
     end 
    end 
    end 
    conds 
end 

. 어떤 조언을 많이 주시면 감사하겠습니다. 약식 모델은 아래와 같습니다.

class Relationship < ActiveRecord::Base 
    belongs_to :product 
    … 
end 

class Product < ActiveRecord::Base 
    has_many :relations 
    has_many :categorizations 
    … 
end 

class Categorizations < ActiveRecord::Base 
    belongs_to :product 
    … 
end 

답변

2

게시 후 즉시 답을 찾지 못했습니다. 대답은 conds = joins(:product)을 다음과 같이 업데이트했습니다.

conds = joins(:product, { :product => :categorizations })