gpt4 book ai didi

ruby-on-rails-3 - 如何使用sunspot rails gem为belongs_to关联编写可搜索方法

转载 作者:行者123 更新时间:2023-12-04 05:51:10 24 4
gpt4 key购买 nike

我有两个模型。一个是品牌,另一个是product_detail。品牌表有 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 类抛出错误未定义的方法品牌

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

最佳答案

它应该是

belongs_to :brand, :dependent=>:destroy

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

在任何情况下,可搜索块应该写为
searchable do
text :name
text :brand do
brand.name
end
end

我希望,这有帮助。

关于ruby-on-rails-3 - 如何使用sunspot rails gem为belongs_to关联编写可搜索方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10022203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com