2

我有兩種模型。一個是品牌,另一個是product_detail。品牌表具有ID和名稱字段,product_details表具有字段ID,名稱,價格,折扣和brand_id。如何使用sunspot_rails編寫用於belongs_to關聯的可搜索方法gem

品牌有很多product_details和product_detail屬於品牌

brand.rb樣子:

class Brand < ActiveRecord::Base 
    has_many :product_details 
end 

和product_details.rb看起來像

class ProductDetail < ActiveRecord::Base 
    belongs_to :Brand, :dependent=>:destroy 
end 

我試圖用太陽黑子做搜索軌道。我想根據品牌名稱和產品名稱與用戶輸入的文字進行搜索。要做到這一點,我已經寫了搜索的方法是這樣的:

class ProductDetail < ActiveRecord::Base 
    belongs_to :brands, :dependent=>:destroy 

    searchable do 
     text :name 
     text :brands do 
     brands.map(&:name) 
     end 
    end 
end 

當我運行耙太陽黑子:REINDEX

它扔了無類

錯誤未定義的方法映射如果改變這樣的代碼

class ProductDetail < ActiveRecord::Base 
    belongs_to :Brand, :dependent=>:destroy 

    searchable do 
     text :name 
     text :Brand do 
     brands.map(&:name) 
     end 
    end 
end 

它拋出一個錯誤未定義的方法品牌,爲product_detail類

請幫助我如何做到這一點。

回答

3

應該

belongs_to :brand, :dependent=>:destroy 

,但你確定要刪除每當您刪除與之相關的product_detail品牌?

在任何情況下,搜索塊然後應該寫成

searchable do 
    text :name 
    text :brand do 
    brand.name 
    end 
end 

我希望幫助。

+0

不,我不想在產品被刪除時刪除品牌。它是我的錯,我有依賴=>在product_details模型中銷燬,而不是品牌模型。我會糾正我的錯誤 – 2012-04-05 09:00:37

+0

非常感謝。我的問題解決了。 – 2012-04-05 09:33:08