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类
请帮助我如何做到这一点。
不,我不想在产品被删除时删除品牌。它是我的错,我有依赖=>在product_details模型中销毁,而不是品牌模型。我会纠正我的错误 – 2012-04-05 09:00:37
非常感谢。我的问题解决了。 – 2012-04-05 09:33:08