在ActionText属性中搜索文本。

huangapple go评论66阅读模式
英文:

Search text in ActionText attribute

问题

我有一个Post模型,这个模型使用ActionText来处理content属性:

has_rich_text :content

现在我有一个简单的搜索功能,我想要在content中搜索文本,所以我有类似这样的代码:

@posts = Post.joins("INNER JOIN action_text_rich_texts ON action_text_rich_texts.record_id = posts.id").where("action_text_rich_texts.content LIKE ?", "%#{search_text}%")

但是这会出现错误:

PG::UndefinedColumn: ERROR:  column action_text_rich_texts.content does not exist

在ActionText属性中搜索文本的正确方法是什么?

英文:

I have a Post model and this model uses ActionText for the attribute content:

has_rich_text :content

Now I have a simple search and I would like to search for text inside content, so I have something like this:

@posts = Post.joins("INNER JOIN action_text_rich_texts ON action_text_rich_texts.record_id = posts.id").where("action_text_rich_texts.content LIKE ?", "%#{search_text}%")

But this gives an error:

PG::UndefinedColumn: ERROR:  column action_text_rich_texts.content does not exist

What is the correct way to search for text in an ActionText attribute?

答案1

得分: 12

这是由rails action_text:install生成的迁移:

# 这个迁移来自 action_text(最初于20180528164100)
class CreateActionTextTables < ActiveRecord::Migration[6.0]
  def change
    create_table :action_text_rich_texts do |t|
      t.string     :name, null: false
      t.text       :body, size: :long
      t.references :record, null: false, polymorphic: true, index: false
      t.timestamps

      t.index [ :record_type, :record_id, :name ], name: "index_action_text_rich_texts_uniqueness", unique: true
    end
  end
end

它告诉我们内容存储在action_text_rich_texts表中,并使用多态关联来链接到记录。

因此,你需要在连接中提供类型和ID,因为可能会有多行具有相同的ID但属于不同的模型:

@posts = Post.joins("INNER JOIN action_text_rich_texts ON action_text_rich_texts.record_id = posts.id AND record_type = 'Post'")

你也可以通过设置关联来避免手动连接:

class Post < ApplicationRecord
  has_rich_text :content
  # 用于直接查询附加的 ActionText
  has_one :action_text_rich_text,
    class_name: 'ActionText::RichText',
    as: :record
end

整个查询如下:

@posts = Post.joins(:action_text_rich_text)
             .where("action_text_rich_texts.body LIKE ?", "%#{search_text}%")
英文:

This is the migration generated by rails action_text:install:

# This migration comes from action_text (originally 20180528164100)
class CreateActionTextTables < ActiveRecord::Migration[6.0]
  def change
    create_table :action_text_rich_texts do |t|
      t.string     :name, null: false
      t.text       :body, size: :long
      t.references :record, null: false, polymorphic: true, index: false
      t.timestamps

      t.index [ :record_type, :record_id, :name ], name: "index_action_text_rich_texts_uniqueness", unique: true
    end
  end
end

It tells us that the contents is stored in action_text_rich_texts and that it uses a polymorphic association to link to the record.

So you need to provide both the type and id in the join as there can be multiple rows with the same id but for different models:

@posts = Post.joins("INNER JOIN action_text_rich_texts ON action_text_rich_texts.record_id = posts.id AND record_type = 'Post'")

You can just setup an association through so you don't have to join manually:

class Post < ApplicationRecord
  has_rich_text :content
  # used to query the attached ActionText directly
  has_one :action_text_rich_text,
    class_name: 'ActionText::RichText',
    as: :record
end

The whole query reads:

@posts = Post.joins(:action_text_rich_text)
             .where("action_text_rich_texts.body LIKE ?", "%#{search_text}%")

答案2

得分: 4

谢谢,Max,我一整天都在使用Ransack来解决这个问题。

所以我添加了:

has_one :action_text_rich_text,
    class_name: 'ActionText::RichText',
    as: :record

然后在我的搜索字段中使用了 _or_action_text_rich_text_body_,它就像魔法一样起作用了。

英文:

Thanks, Max, I have been struggling with this all day using Ransack.

So I added:

has_one :action_text_rich_text,
class_name: 'ActionText::RichText',
as: :record

And then used or_action_text_rich_text_body in my search field and it worked like a charm.

huangapple
  • 本文由 发表于 2020年1月3日 16:37:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定