“Searching” is the most common feature of most of the web applications irrespective of size, small or enterprise grade.

Searching with Ransack in Ruby on Rails Application

Ransack is a very powerful and feature-rich gem used widely by Rails community to implement advanced search capability in a Ruby on Rails application. You can create simple as well as advanced search forms for with this gem.

Let’s build a small search app using simple and advanced search options.

First of all, add Ransack gem to your gemfile and bundle.

Gemfile

gem 'ransack', github: 'activerecord-hackery/ransack'

$ bundle

Let’s first create a scaffold for Student details which we can use further to explore different search options provided by Ransack.

$ rails g scaffold Student name age:integer marks:integer

After generating Student scaffold you have below code in your student index view

In app/views/students/index.html.erb

As we are going for search directly we don’t need a new student link in view so remove below link from index view,

In app/views/students/index.html.erb [Remove below line from index.html]

<%= link_to 'New Student', new_student_path %>

To add few student records in database add below lines of code in your seed file

In db/seeds.rb

To create a database with desire student schema with few records write below command in terminal.

$ rails db:setup

Above command will create a database with scaffold migration and student records.

Ransack provides form helper search_form_for which you can think of as an extension to form_for with some metaprogramming magic to give you a powerful DSL specifying predicates. When the form generated by search_form_for is submitted, the handler(controller) creates Ransack::Search and validates it.

Here we are searching by name in the Student table. So add below code for the search to students’ index view page above the table or elsewhere you want to display searching form in the view.

In app/views/students/index.html.erb

Here we have name field in our table, then we have to mention it like “name_cont” in helper search_form_for. Here we used “_cont” matcher which means Contains value in “name” field. If we did not follow the conventions then ransack will return the full list of data without proper searching.

Here is how our controller will look like.

app/controllers/students_controller.rb

Here,  @q is a ransack::search object using a parameter which is in params[:q].

And #result method of ransack returns an ActiveRecordRelation object with matching result which we can utilize to display in view files.

The above code gives the result of search like below:

Searching with Ransack in Ruby on Rails

Ransack provides different search matches as below. We can apply any of them based on our requirement.

ransack custom sort

Here is the list of all available matches which you can as per the needs.

We can easily integrate sorting functionality as well using #sort_link. See below code for that:

app/views/students/_student_list.html.erb

After adding above sort link it will look like below.

rails ransack

Now, let’s implement a bit advanced search tool.

To achieve this, we have to add a few more lines of code in search_form_for file like given below:

app/views/students/index.html.erb

For an advanced search, ransack uses nested structures of input fields. It groups all conditions and each of that group has an attribute name and value which is a search value on a specific attribute.

So the above code results in the following view:

Searching and Sorting on Rails with Ransack

The first dropdown contains table attributes. This is the default behavior. The middle one is for predicates for ransack matchers and text field for values to be searched.

Here we can also restrict to few attributes on which we want to give permission for searching. Just configure table fields name on its model file as below to customize it.

app/models/student.rb

Here we are allowing to search on name and marks fields only.

That’s it! You can build your complex search forms with complex queries using Ransack easily.

You can access full codebase in this git repo. I would highly recommend checking wiki for all different search options available.

Happy coding!

Click here for more details


At BoTree Technologies, we build enterprise applications with our RoR team of 25+ engineers.

We also specialize in RPA, AI, Python, Django, JavaScript and ReactJS.

Consulting is free – let us help you grow!