gpt4 book ai didi

ruby-on-rails - 如何在 Rails 的 Action Text 中包含 Pgsearch

转载 作者:行者123 更新时间:2023-12-02 00:55:35 26 4
gpt4 key购买 nike

我正在将 pgsearch 添加到 Rails 6 Action Text 中,但不确定在 RichText 模型中包含 pgsearch 的最佳技术。我似乎无法在不破坏模型的情况下修补模型。我确实通过完全替换模型来让它工作,但显然不想那样做。任何想法如何使这项工作?这是我目前的模型:

include PgSearch

class ActionText::RichText < ActiveRecord::Base
self.table_name = "action_text_rich_texts"

multisearchable :against => :body

serialize :body, ActionText::Content
delegate :to_s, :nil?, to: :body

belongs_to :record, polymorphic: true, touch: true
has_many_attached :embeds

before_save do
self.embeds = body.attachments.map(&:attachable) if body.present?
end

def to_plain_text
body&.to_plain_text.to_s
end
delegate :blank?, :empty?, :present?, to: :to_plain_text

end

最佳答案

@dirk-sird-de-vries 的答案对我来说并不是很明显。因此,我将添加一个示例以供我学习和将来引用。

class Book < ApplicationRecord
include PgSearch

has_rich_text :chapter

pg_search_scope :book_search, associated_against: {
rich_text_chapter: [:body]
}
end

当您搜索 book.chapter在命令行中,您将获得以下信息:
#<ActionText::RichText id: 1, name: "chapter", body: #<ActionText::Content "<div class=\"trix-conte...">, record_type: "Book", record_id: 1, created_at: "2019-12-31 00:00:00", updated_at: "2019-12-31 00:00:00">

因此, associated_against 需要是 body .

关于ruby-on-rails - 如何在 Rails 的 Action Text 中包含 Pgsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54506431/

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