将pg

编程入门 行业动态 更新时间:2024-10-28 06:35:32
本文介绍了将pg_search与多态关联一起使用时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的模型有以下范围:

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.无需多说,在正常情况下,所有者关系是有效的.无论如何都是在这样的模块中定义的:

So if I call something like Cloth.full_search('shirt') works fine, but if I add owner: [:name] to the associated_against hash, it throws NameError: uninitialized constant Cloth::Owner. Nedless to say that the owner relationship in normal circumstances is working. In any case is defined in a module like this:

module Ownerable extend ActiveSupport::Concern included do belongs_to :owner, :polymorphic => true end

任何线索可能是什么?预先感谢

Any clue what thing could be? Thanks in advance

推荐答案

我是pg_search的作者和维护者.

I'm the author and maintainer of pg_search.

不幸的是,在纯SQL中不可能朝这个方向遍历多态关联,因此使用pg_search进行搜索是不可能的.

Unfortunately, it's not possible to traverse a polymorphic association in this direction in pure SQL, so doing that search is not possible with pg_search.

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

One thing you could do is calculate the text from the other records and cache it to a column on the Cloth table, then search against that instead. You'd have to be careful to update it whenever either the polymorphic foreign key changes on Cloth or the content changes on the Owner record.

希望我可以改善错误消息,以免造成混乱.感谢您指出这一点.

Hopefully I can improve the error message so that it's not so confusing. Thanks for pointing this out.

更多推荐

将pg

本文发布于:2023-08-04 01:29:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1291689.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:pg

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!