Rails RANSACK on Rich Text

crazyjacky · October 08, 2022 · 329 hits

最近把论坛的搜索从 SOLR 切换到了 RANSACK,原因 SOLR 在过去的大半年时间挂了两次,并且目前搜索使用并不多,而且数据量也很小,觉得用 RANSACK 反而更稳定。 然后刚切到 RANSACK 就碰到了 rich text 默认无法搜索的情况。具体解决办法如下:

原 Model:

class Post < ApplicationRecord
  has_rich_text :content
end

修改后如下

class Post < ApplicationRecord
  has_rich_text :content
  has_one :content, class_name: 'ActionText::RichText', as: :record
end

这样就可以如下在 view 里面搜索了

f.text_field :title_or_content_body_cont
No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.