Blog

Simplify Data Filtering in Ruby on Rails with Ransack

George Tobias
November 1, 2023

One of the common tasks in web applications is filtering and searching through data. This can be complex and time-consuming to implement from scratch, but luckily, there’s a fantastic gem called Ransack that simplifies this process.

What is Ransack?

Ransack is a versatile and powerful search and filter library for Rails applications. It provides a user-friendly interface to create complex search queries, including sorting and filtering of data. The gem leverages ActiveRecord and Arel under the hood, making it a robust solution for searching and filtering records in your application.

Why Use Ransack?

Here are some reasons why Ransack is a great choice for filtering data in your Ruby on Rails application:

  • Simplicity. Ransack simplifies the process of building complex search and filter functionality. It eliminates the need to write extensive SQL queries manually.

  • Customization. Ransack is highly customizable. You can easily configure it to match your application’s specific needs, including custom search fields, operators, and predicates.

  • User-Friendly Interface. Ransack provides an intuitive interface for end-users to create filters and search queries without needing to know SQL.

  • Performance. Since Ransack generates SQL queries, it ensures efficient and optimized database performance.

Getting Started with Ransack

To get started with Ransack, you need to add it to your Rails project by including it in your Gemfile and running bundle install:


gem 'ransack'

Basic Usage

Let’s take a simple example of a product listing page and see how Ransack simplifies filtering. We have a Product model with attributes like name, price, and category.

  • Ensure that Product attributes are whitelisted in your model:

# app/models/product.rb

def self.ransackable_attributes(_auth_object = nil)
  [
    "name",
    "price",
    "category"
  ]
end
  • In your controller, set up Ransack like this:

def index
  @q = Product.ransack(params[:q])
  @products = @q.result
end
  • Build a search form in your view with Ransack’s search_form_for helper and a variety of available predicates (i.e. cont, eq, gteq, lteq, etc.):

<%= search_form_for @q do |f| %>
  
<%= f.label :name_cont, "Product Name" %> <%= f.text_field :name_cont %>
<%= f.label :price_gteq, "Price between" %> <%= f.text_field :price_gteq %> <%= f.label :price_lteq, "And" %> <%= f.text_field :price_lteq %>
<%= f.label :category_eq, "Category" %> <%= f.collection_select :category_eq, Category.all, :id, :name, include_blank: true %>
<%= f.submit "Search" %>
<% end %>
  • In your view, display the filtered results:

<% @products.each do |product| %>
  

<%= product.name %>

<%= product.price %>

<%= product.category %>

<% end %>

That’s it! You now have a functioning search and filter feature for your products.

Advanced Features

Ransack offers various advanced features like sorting, custom predicates, and search association to further enhance your filtering capabilities.

You can also build more complex search forms with ease with the built-in form builder which lets you dynamically construct your query.

Some Pitfalls

Since Ransack is just another layer of abstraction between the database and your logic, it lives (and dies) along with your database performance. Therefore, best practices in designing your database should still be applied (i.e. resolving N+1 queries, indexing, etc.)

Additionally, things can become quite slow for full text searches. If you use Ransack features such as name_cont (which is translated to ILIKE with two wildcards), the resulting query response can become sluggish on large data sets depending on the SQL server you are using.

Conclusion

Ransack is a powerful gem that simplifies data filtering and searching in Ruby on Rails applications. It is a valuable tool that can save you time and effort. But, as always, YMMV when choosing the right tools for your application. Thanks for reading & happy coding!

No matter your software problem, we provide a complete solution to scope, design, develop, test, host, maintain and support it 24/7/365. Contact us to find out how can we bring your ideas to reality!