Search forms and sortable columns for your Rails app, in plain Ruby and ERB
open sourceMITself-hostablefree
Ransack adds search forms and sortable columns to Rails apps without a search server: Post.ransack(params[:q]) turns parameters named after a column and a predicate (title_cont, price_gteq, deleted_at_null) into Active Record SQL, and search_form_for and sort_link build the form and the column headers. It searches and sorts through associations by name, nests AND and OR groups in advanced mode, and lets you define your own searchable attributes (ransackers) in Arel. Nothing is searchable until a model allowlists its attributes, associations and scopes. Ernie Miller created it, volunteers maintain it with support from Open Collective and Tidelift, and ActiveAdmin builds on it.
Problems it solves
- add a search form with filters to the list pages of my Rails app
- let users sort a table by clicking its column headers
- filter records by any column from query string parameters without writing a scope for each
- search by a related model's field, like posts by their author's name
- build nested AND/OR filter conditions from a web form
- I don't want to run Elasticsearch just to filter a list of records
- make sure users can't search or sort on columns they shouldn't see
- filter by date ranges, greater than, starts with or is empty on an index page
- search on a computed value such as a full name built from two columns
- an API endpoint that accepts flexible filter and sort parameters for Active Record models
- keep the filters and sort order in the URL so a search can be bookmarked and shared
Features
- predicates in parameter names: eq, cont, start, end, lt, gteq, in, null, present and more
- search_form_for and sort_link view helpers
- search and sort through associations
- advanced mode with nested AND and OR groups
- ransackers: custom searchable attributes in Arel
- allowlists per model (ransackable_attributes, associations, scopes) or strong parameters
- PostgreSQL, MySQL, MariaDB and SQLite
Capabilities
| Has an API | plan not known |
|---|
