0

I'm using simple search in my blog:

post.rb

has_rich_text :content

  def self.search(search)
    where('title ILIKE ? OR content ILIKE ?', "%#{search}%", "%#{search}%")
  end

post_controller.rb

@posts = if params[:search]
  Post.search(params[:search]).order('created_at DESC')
  else
  Post.all.order('created_at DESC')
  end

But after I added the trix editor, post content search stopped working(for titles - works)

SsPay
  • 161
  • 9
  • 1
    What does the field content contain in your posts table? It could be the format that trix editor uses or it might be you are actually using ActiveText which will not be using the content field at all. – Ben Trewern Dec 19 '19 at 16:30
  • i use ActiveText – SsPay Dec 19 '19 at 18:33
  • If you are using this way shouldnt it be `where('title ILIKE ? OR content ILIKE ?', "'%#{search}%'", "'%#{search}%'")` ? – Dawid Dec 19 '19 at 19:44
  • 1
    If you are using action_text then the added content is not in your original table but in a table called "action_text_rich_texts". You may be able to join between that and you current table to do the search but I'd be tempted to use a before_save callback and extract the plain text from the rich text column using content.to_plain_text. Then you could do your search on that as you do now. – Ben Trewern Dec 19 '19 at 20:48
  • @BenTrewern thank you, you really helped me – SsPay Dec 19 '19 at 22:07

0 Answers0