gpt4 book ai didi

ruby-on-rails - 使用具有多态关联的 pg_search 的问题

转载 作者:行者123 更新时间:2023-11-29 14:23:02 24 4
gpt4 key购买 nike

我的模型有以下范围:

class Cloth < ActiveRecord::Base
include Ownerable
has_many :cloth_tags, :dependent => :destroy

pg_search_scope :full_search,
associated_against: {
cloth_tags: [:name, :brand_name]
},
against: [:name, :description],
ignoring: :accents,
using: {
tsearch: {
dictionary: "spanish",
any_word: true
}
}

因此,如果我调用类似 Cloth.full_search('shirt') 的东西,效果很好,但如果我将 owner: [:name] 添加到 associated_against 哈希,它会抛出 NameError: uninitialized constant Cloth::Owner。不用说,正常情况下所有者关系是有效的。在任何情况下都是在这样的模块中定义的:

module Ownerable

extend ActiveSupport::Concern

included do
belongs_to :owner, :polymorphic => true
end

有什么线索吗?提前致谢

最佳答案

我是 pg_search 的作者和维护者。

不幸的是,不可能在纯 SQL 中沿此方向遍历多态关联,因此无法使用 pg_search 进行搜索。

您可以做的一件事是计算其他记录中的文本并将其缓存到 Cloth 表的列中,然后改为搜索该列。每当 Cloth 上的多态外键发生变化或 Owner 记录上的内容发生变化时,您都必须小心更新它。

希望我可以改进错误消息,使其不再那么困惑。感谢您指出这一点。

关于ruby-on-rails - 使用具有多态关联的 pg_search 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15364422/

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