2

我有两个模型。一个是品牌,另一个是product_detail。Brands 表有 id 和 name 字段,product_details 表有 id、name、price、discount 和 brand_id 字段。

品牌有很多 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

当我运行 rake sunspot:reindex

它为 nil 类抛出错误未定义的方法映射

如果像这样更改代码

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

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

它为 product_detail 类抛出错误未定义的方法品牌

请帮助我如何做到这一点。

4

1 回答 1

3

它应该是

belongs_to :brand, :dependent=>:destroy

但是您确定要在删除与之关联的 product_detail 时删除该品牌吗?

在任何情况下,可搜索块应写为

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

我希望,这会有所帮助。

于 2012-04-05T08:11:38.543 回答