Ransack

Ransack is a rewrite of MetaSearch. Ransack enables the creation of both simple and advanced search forms for your Ruby on Rails application. Ransack works out-of-the-box with Active Record

Usage

Ransack can be used in one of two modes, simple or advanced.

Simple Mode

This mode works much like MetaSearch, for those of you who are familiar with it, and requires very little setup effort. In MetaSearch, things to note:

The default param key for search params is now:q, instead of :search. This is primarily to shorten query strings, though advanced queries will still run afoul of URL length limits in most browsers and require a switch to HTTP POST requests. This key is configurable.

Advanced Mode

“Advanced” searches (ab)use Rails’ nested attributes functionality in order to generate complex queries with nested AND/OR groupings, etc. This takes a bit more work but can generate some pretty cool search interfaces. A notable drawback with these searches is that the increased size of the parameter string will typically force you to use the HTTP POST method instead of GET.

The primary method of searching in Ransack is by using what is known as Predicates.

Predicates are used within Ransack search queries to determine what information to match. Below is a list of the built-in predicates of Ransack and their opposites. 

  • eq (equals)
  • matches
  • lt (less than)
  • lteq (less than or equal to)
  • in
  • cont
  • cont_any (contains any)
  • cont_all (contains all)
  • i_cont
  • i_cont_any
  • i_cont_all
  • start (starts with)
  • end (ends with)
  • true
  • false
  • present
  • null