pau*_*ulz 6 ruby-on-rails pg-search
我正在将 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
Run Code Online (Sandbox Code Playgroud)
小智 6
我已经使用 Associated_against 解决了 ActionText+PgSearch 难题,不完全是您所要求的,但也许它对您的问题有用。
class Article < ApplicationRecord
include PgSearch
has_rich_text :content
pg_search_scope :search, associated_against: {
rich_text_content: [:body]
}
end
Run Code Online (Sandbox Code Playgroud)
一切顺利!
@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
Run Code Online (Sandbox Code Playgroud)
当你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">
Run Code Online (Sandbox Code Playgroud)
因此, associated_against 需要是body。