Filterable Gem

Maja Radovic
Jul 10, 2018 · 1 min read

Here is a useful Ruby gem from Toschas!

Filterable gem provides an easy way to filter content, and it can be integrated in any application that uses Active Record.

Filtering displayed content is a part of almost any web app.

In Rails (or any other ActiveRecord applications) it’s usually done with scopes:

class Post < ActiveRecord::Base
scope :by_published, ->(value) { where(published: value) }
scope :by_author_id, ->(author_id) { where(author_id: author_id) }

which then allows you to do something like the following:


The number of scopes tend to grow big in an average-sized Rails app and it becomes pretty tiring and repetitive work to maintain them. The model’s fields which are allowed to be filtered by must also be configured manually.

Our solution: .

Filterable gem automates the process of defining and using filters on your models by providing a simple API. It returns an ActiveRecord Collection so it can be chained to other methods (same as using scopes).

class Post < ActiveRecord::Base
filter_by: :published, :author_id

and then in the controller, you can simply do the following:

def index
# allowed filters being defined like this
def allowed_filters
params.permit(:by_published, :by_author_id)

Filterable also supports filtering by related model’s fields, comparing numeric values and dates and defining custom filters for any special case you may have.

You can check it out in more detail .


Toschas Blog

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store