Refining the Ransack gem to properly search for multiple strings

Problem:
Ransack will only search for one keyword or phrase such as, "Jumpity Jump", without quotations. Using quotations or commas such as in "Jumpity Jump" or, "Jumpity, Jump" (with or without quotations). returns a blank result.

Test conditions:
Randomly selected records were edited to include varied words/phrases in searched fields, such as in the following examples:

  • jump
  • jumpity jump
  • jimpity jump jump
  • rabbit jumpity
  • jumpity rabbit
Solution:
Custom predicates added to initializer based on Stack Overflow answer linked at end.

Using "cont_all"

 This returns records if:


  • quotation or commas​ are not used

  • Search phrase is found

  • Search word is found 


Example Sentence: "rabbit has toes that go jumpity jump jump"

Search phrases:

  1.  rabbit (match)

  2. Rabbit (match)

  3. RABBIT (match)

  4. rab (match)

  5. bit (match)

  6.  rabbit toes (no match)

  7. "rabbit toes" (no match)

  8.  rabbit toes weasel (no match)

  9.  rabbit has (match)

  10. rabbit, jump (no match)

  11. "rabbit has" (no match)

  12.  rabbit, has (no match)

  13. jumpity jump (no match)

  14. jump jump (match)

Using "cont_any"

 Is supposed to be different from "cont_all" but appears to work the same. 



Example Sentence: "rabbit has toes that go jumpity jump jump"

Search phrases:


  1.  rabbit (match)

  2. Rabbit (match)

  3. RABBIT (match)

  4. rab (match)

  5. bit (match)

  6.  rabbit toes (no match)

  7. "rabbit toes" (no match)

  8.  rabbit toes weasel (no match)

  9.  rabbit has (match)

  10. rabbit, jump (no match)

  11. "rabbit has" (no match)

  12.  rabbit, has (no match)

  13. jumpity jump (no match)

  14. jump jump (match)

Using "has_any_term"

Example Sentence: "rabbit has toes that go jumpity jump jump"

Search phrases:


  1.  rabbit (match)

  2. Rabbit (match)

  3. RABBIT (match)

  4. rab (match)

  5. bit (match)

  6.  rabbit toes (match)

  7. "rabbit toes" (no match)

  8.  rabbit toes weasel (match)

  9.  rabbit has (match)

  10. rabbit, jump (match)

  11. "rabbit has" (match)

  12.  rabbit, has (match)

  13. jumpity jump (match)

  14. jump jump (match)

Using "has_every_term"

Example Sentence: "rabbit has toes that go jumpity jump jump"

Search phrases:


  1.  rabbit (match)

  2. Rabbit (match)

  3. RABBIT (match)

  4. rab (match)

  5. bit (match)

  6.  rabbit toes (match)

  7. "rabbit toes" (no match)

  8.  rabbit toes weasel (no match)

  9.  rabbit has (match)

  10.  rabbit, jump (match)

  11. "rabbit has" (match)

  12.  rabbit, has (match)

  13.  jumpity jump (match)

  14.  jump jump (match)

Conclusion

The built-in predicates don't seem to work as they should according to the documentation.  The custom predicates added from Stack Overflow works as expected, but most likely also introduce performance issues that will need to be addressed in time.

guide
For use with trusted/cleaned data. Add to application.rb def truncate_and_link(text,options ={}) length = options[:length] return text if length.blank? url = options[:url] || '#' output = raw text.truncate_words(length) output += link_to('more', url) if text.size > length output.html_safe end Adapted from: http://itiansrock.blogspot.com/2013/01/truncate-and-link-text-helper-in-rails.htmlmore
guide
Recently, while creating a subscription checkout form using Stripe Elements, I wanted to list the plans along wth their pricing and details. The form uses a collection input to list the plans as radio buttons, but the methods in this guide should work for checkboxes and selects.  Here is the input statement that we are working with for starters: <%= f.input :plan_id,   collection: Plan.published, as: :radio_buttons %> Currently, this will display the following label and radio buttons (Note that this label is provided using I18n): Please select your plan: ○ plan 1 ○ plan 2 I already have text with instructions...more
guide
The ability to like or favorite posts is very common on websites. This guide will show you how to add this functionality to your Rails 5 application without adding unnecessary bloat from gems. This guide assumes that you have already implemented posts (or similar) and users to your application and will not cover these steps. Let's go over some design considerations before we begin. While everyone can view a post, only authenticated users can like posts by clicking a link or icon.  We're keeping it positive by just tracking likes.more
guide
Font Awesome 5 SVG icons use javascript to find <i> tags with an icon class and replaces them with an <svg> tag. Turbolinks online displays these icons on initial page load and/or refresh, however when following a link, Turbolinks reloads the page and not the icons. This guide assumes that you have already loaded font awesome into your project using your preferred method. For what its worth, this guide uses the font awesome CDN more
guide
Assuming that you have Rails and PostgreSQL already installed on your workstation, follow the steps for a new or existing Ruby on Rails app according to your needs. more
guide
Basic steps to create a new app using Rails API with a React frontend, then deploy on Heroku.