How to Add Search Functionality Into a Rails API Application

Feature thumb search

This tutorial walks through how to implement search into a Rails API application. The tools I'll be using are:

  • Rails 5.2 API only Ruby on Rails application
  • pg_search gem
  • Postgres database

Step 1 Install the pg_search Gem

gem 'pg_search'

 

Step 2 Run bundle install after adding the gem

 

Step 3 Add the Search Instructions to the Model to Search

 

Step 4 Call the Scope from the Controller and Return JSON

 

Run a query like this (example using Axios):