2009-12-22 3 views
2

모델이를 통해 : 제품을생각 스핑크스, 레일, : has_many => ...이

has-many product-categories, :through => ... 

질문 1)

내가 사용해야합니다 스핑크스 생각으로 내가 지수에게 많은 관계로 많은 작업을 수행하는 방법 있다?

질문 2)입니다 어떻게 이런 일이 컨트롤러

전에서 검색. Product.search params [: search-params], : 조건 => {some_conditions}

답변

2

나는 has_many에서 이것을 시도하지 않았다. 그렇게하면 화염에 나를 쏴 버린다. 그러나 나는 왜 보이지 않는다. 이것은 당신을 위해 또한 작동하지 않을 것이다. (나는 has_many 연관에 그것을 사용하고있다.) 당신은 기본적으로 당신의 연관을 인덱스 정의에서 사용한다. 그런 다음 해당 모델에 대한 검색은 하위 레코드도 검색합니다. 뷰에서

@products = Product.search(params[:query] || '') 
#params[:query] is simply the search string, I can't remember if you need to sanitize this, I would always assume you do unless you find out otherwise 

: 당신은 이미이없는 경우

@products.each do |p| 
    p.categories.each do |cat| 
    end 
end 

내가 매우 peepcode에 사용할 수있는 생각 - 스핑크스 책을 추천 할 것 컨트롤러에서

class Product < ActiveRecord::Base 
    has_many :product_categories 

    define_index do 
    indexes a_product_field_to_index 
    indexes product_categories.name, :as => :categories 
    end 
end 

: https://peepcode.com/products/thinking-sphinx-pdf

희망이 있습니다.

관련 문제