Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • pg_search gem integration in rails application

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 861
    Comment on it

    If we are using PostgreSQL RDBMS in rails application and there are need to search functionality, pg_search gem is perfect for that. It has easy implementation and having rich methods for the search functionality.

    Note: pg_search gem supports only PostgreSQL RDBMS

     

    The pg_search provides two different searching criteria in the application

    Search Scopes: It is basically used in developing functionality like autocompleters and searching records from lists.

    Multi Search: In multi search, an application uses the one global search index approach.

     

    Here we will search results from the articles list against the article title & article content.

     

    Step1: Add pg_search gem in Gemfile

    Step2: Run bundler

    Step3: Add search field in article index page

    Step4: Include pg_search module in model

    Step5: Create Scope for search

    Step6: Invoke in article_controller

     

    Step1: Add pg_search gem in Gemfile

    Path: my_app/Gemfile

    gem 'pg_search'

     

    Step2: Run bundler

    Now run bundler using command

    bundle install

     

    Step3: Add search field in article index page

    In index.html.erb we add the search form bu which user can search articles.

     

    Path: my_app/app/views/articles/index.html.erb

    <%= form_tag(artiles_index_path, method: "get") do %>
      <%= label_tag(:query, "Search for:") %>
      <%= text_field_tag(:query) %>
      <%= submit_tag("Search") %>
    <% end %>
    
    <table>
      <tr>
        <th>Title</th>
        <th>Content</th>
      </tr>
      <% @articles.each do |article| %>
        <tr>
          <td><%= article.title %></td>
          <td><%= article.content %></td>
        </tr>
      <% end %>
    </table>
    

     

     

    Step4: Include pg_search module in model

    Then modify article.rb model like below:

     

    Path: my_app/app/models/article.rb

    class Article < ActiveRecord::Base
    	include PgSearch
     	pg_search_scope :search_title, :against => [:title, :content]
    end

     

     

    'include PgSearch ' this line include the pgSerach module into article model

    ' pg_search_scope' this is defined scope name i.e 'search_title' and  ' against' used to set the table columns for the search

     

     

    Step6: Invoke scope in article_controller

    Here we will call the scope for searching in controller

     

    Path: my_app/app/controllers/articles_controller.rb

    class ArticlesController < ApplicationController
    
      def index
          if params[:query].present?
              @articles = Article.search_title(params[:query])
          else
              @articles = Article.all
          end
       end
    end
    

     

    In the above code, first we checked the search query string, if present, then called the search scope to get records otherwise it will go into else condition. We will discuss other searching features of pg_search gem in next blog.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: