Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I search Action Text with Ransack?

Ryan Carter asked in Rails

I have set up a basic search using Ransack but I don't understand how to get it to search Action Text. This is how it is implimented in the search_form_for:

f.search_field :title_or_rcontent_or_city_or_state_i_cont

The rcontent is the Action Text portion.

Reply

For anyone else searching for this issue, I found a solution.

Add this to your model -
has_one :action_text_rich_text,
class_name: 'ActionText::RichText',
as: :record

And then add or_action_text_rich_text_body to your search field, because "body" is what is created when the ActionText migration is created.

Reply

Thank you! I am now getting duplicates in my searches though. Did that happen to you?

Reply

Let's say you have an ActionText field like so:

has_rich_text :body

You can join that without defining another association like so:

joins(:rich_text_body)

One option could be to do a full text search in PostgreSQL:

joins(:rich_text_body).where("to_tsvector(action_text_rich_texts.body) @@ websearch_to_tsquery('english', ?)", phrase)

One of the reasons I went with this approach was to help make the HTML-formatted text a bit easier to search through, and give the user a bit more flexibility. For example, since I used the websearch_to_tsquery, it supports things like quoting to search for a phrase, etc.

EDIT Heh just noticed this is a rather old thread. Anyway, maybe this will help someone in the future!

Reply

Thanks, I will play around with it and see what I can figure out.

Reply

I'll share what I did today to solve for this, which is really close to what Ryan did. The model, among other attributes, has a title and a rich text description like so:

has_rich_text :description

I wanted to be able to search across both attributes. I had the title one worked out already but to get the rich text description into the mix I then added the following line:

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

Which then allowed me to do this in the form:

f.text_field :title_or_description_body_cont

Hope this adds to the help that was already here and thanks to the other folks who contributed to this thread!!

Reply

that of @cjilbert504 worked for me, thanks alot

Reply

I found this beautiful post from thoughtbot, about this. It @Mylan Connolly solution, going a bit further.

Instead of searching directly in the ActionText body field, which has HTML tags, it's better to convert that field to plain text, and perform the search in the converted field.

Reply

Duplicates in the search result can be removed using the distinct option like so:

@pagy, @discussions = pagy(@q.result(distinct: true))

Reply

I second @miguel's recommendation for the post from Thoughtbot. Insanely great implementation!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.