英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论