Search and Autocomplete in Rails Apps

Adding search to Rails apps

Searching is one of the most common features found on virtually any website. There are numerous solutions out there for easily incorporating search into your application, but in this article I’ll discuss Postgres’ native search in Rails applications powered by the pg_search gem. On top of that, I’ll show you how to add an autocomplete feature with the help of the select2 plugin.

I’ll explore three examples of employing search and autocomplete features in Rails applications. Specifically, this article covers:

  • building a basic search feature
  • discussing additional options supported by pg_search
  • building autocomplete functionality to display matched user names
  • using a third-party service to query for geographical locations based on the user’s input and powering this feature with autocomplete.

The source code can be found at GitHub.

The working demo is available at

Getting Started

Go ahead and create a new Rails application. I’ll be using Rails 5.0.1, but most of the concepts explained in this article apply to older versions as well. As long as we’re going to use Postgres’ search, the app should be initialized with the PostgreSQL database adapter:

rails new Autocomplete --database=postgresql

Create a new PG database and setup config/database.yml properly. To exclude my Postgres username and password
from the version control system, I’m using the dotenv-rails gem:


# ...
group :development do
  gem 'dotenv-rails'

To install it, run the following:

$ bundle install

and create a file in the project’s root:


PG_USER: 'user'
PG_PASS: 'password'

Then exclude this file from version control:



Your database configuration may look like this:


  adapter: postgresql
  database: autocomplete
  host: localhost
  user: < %= ENV['PG_USER'] %>
  password: < %= ENV['PG_PASS'] %>

Now let’s create a table and populate it with sample data. Rather than invent anything complex here, I’ll simply generate a users table with name and surname columns:

$ rails g model User name:string surname:string
$ rails db:migrate

Our sample users should have distinct names so that we can test the search feature. So I’ll use the Faker gem:


# ...
group :development do
  gem 'faker'

Install it by running this:

$ bundle install

Then tweak the seeds.rb file to create 50 users with random names and surnames:


50.times do
  User.create({name: Faker::Name.first_name,
              surname: Faker::Name.last_name})

Run the script:

$ rails db:seed

Lastly, introduce a root route, controller with the corresponding action and a view. For now, it will only display
all the users:


# ...
resources :users, only: [:index]
root to: 'users#index'


class UsersController < ApplicationController
  def index
    @users = User.all


  < %= render @users %>


  < %= %> < %= user.surname %>

That’s it; all preparations are done! Now we can proceed to the next section and add a search feature to the app.

Continue reading %Search and Autocomplete in Rails Apps%

Source: Sitepoint