2011-06-06 21:32:03 -04:00
# Ransack
2013-12-01 23:01:10 -05:00
[![Build Status ](https://travis-ci.org/activerecord-hackery/ransack.png )](https://travis-ci.org/activerecord-hackery/ransack)
2013-01-31 00:46:18 -05:00
2013-12-18 14:45:49 -05:00
Ransack is a rewrite of [MetaSearch ](https://github.com/activerecord-hackery/meta_search ). While it
2011-06-06 21:32:03 -04:00
supports many of the same features as MetaSearch, its underlying implementation differs
greatly from MetaSearch, and _backwards compatibility is not a design goal._
2012-05-11 02:48:02 -04:00
Ransack enables the creation of both simple and [advanced ](http://ransack-demo.herokuapp.com/users/advanced_search )
2013-12-18 14:45:49 -05:00
search forms against your application's models (demo source code [here ](https://github.com/activerecord-hackery/ransack_demo )). If you're looking for something that
2011-06-11 14:22:41 -04:00
simplifies query generation at the model or controller layer, you're probably not looking
2012-03-29 10:50:36 -04:00
for Ransack (or MetaSearch, for that matter). Try
2013-12-09 05:00:21 -05:00
[Squeel ](https://github.com/activerecord-hackery/squeel ) instead.
2011-06-06 21:32:03 -04:00
## Getting started
In your Gemfile:
2013-05-04 01:13:46 -04:00
```ruby
2013-12-18 14:17:06 -05:00
gem "ransack" # Last officially released gem (Rails 3.x and 4.0)
2013-05-04 01:13:46 -04:00
```
2012-04-11 14:20:46 -04:00
2013-12-18 14:17:06 -05:00
Or if you want to use the bleeding edge (Rails 3.x and 4.0):
2012-04-11 14:20:46 -04:00
2013-05-04 01:13:46 -04:00
```ruby
2013-12-06 18:02:40 -05:00
gem "ransack", github: "activerecord-hackery/ransack" # Track git repo
```
2013-12-18 14:17:06 -05:00
A lighter, faster, latest-commits version of Ransack dedicated to Rails 4.0 is available on the "rails-4" branch:
2013-12-06 18:02:40 -05:00
```ruby
gem "ransack", github: "activerecord-hackery/ransack", branch: "rails-4"
2013-07-02 05:27:14 -04:00
```
2013-12-25 13:56:10 -05:00
Ransack is not yet compatible with Rails 4.1, due to changes and refactoring in ActiveRecord that break it and the [Polyamorous ](https://github.com/activerecord-hackery/polyamorous ) gem that Ransack depends upon. Feel free to contribute a PR to fix it!
2013-12-18 14:17:06 -05:00
2011-06-06 21:32:03 -04:00
## 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.
If you're coming from MetaSearch, things to note:
1. The default param key for search params is now `:q` , instead of `:search` . This is
2012-03-29 10:50:36 -04:00
primarily to shorten query strings, though advanced queries (below) will still
2012-10-06 17:14:47 -04:00
run afoul of URL length limits in most browsers and require a switch to HTTP
2012-04-11 15:17:15 -04:00
POST requests. This key is
2013-12-18 14:17:06 -05:00
[configurable ](https://github.com/activerecord-hackery/ransack/wiki/Configuration ).
2013-12-18 14:45:49 -05:00
2011-06-06 21:32:03 -04:00
2. `form_for` is now `search_form_for` , and validates that a Ransack::Search object
is passed to it.
2013-12-18 14:45:49 -05:00
2011-06-06 21:32:03 -04:00
3. Common ActiveRecord::Relation methods are no longer delegated by the search object.
Instead, you will get your search results (an ActiveRecord::Relation in the case of
2013-08-06 13:47:59 -04:00
the ActiveRecord adapter) via a call to `Search#result` . If passed `distinct: true` ,
2011-06-11 14:22:41 -04:00
`result` will generate a `SELECT DISTINCT` to avoid returning duplicate rows, even if
conditions on a join would otherwise result in some.
2012-03-29 10:50:36 -04:00
2011-06-11 14:22:41 -04:00
Please note that for many databases, a sort on an associated table's columns will
2013-08-06 13:47:59 -04:00
result in invalid SQL with `distinct: true` -- in those cases, you're on your own,
2011-06-11 14:22:41 -04:00
and will need to modify the result as needed to allow these queries to work. Thankfully,
9 times out of 10, sort against the search's base is sufficient, though, as that's
generally what's being displayed on your results page.
2011-06-06 21:32:03 -04:00
In your controller:
2013-05-04 01:13:46 -04:00
```ruby
def index
@q = Person.search(params[:q])
2013-08-06 13:47:59 -04:00
@people = @q .result(distinct: true)
2013-05-04 01:13:46 -04:00
end
```
2011-06-06 21:32:03 -04:00
In your view:
2013-05-04 01:13:46 -04:00
```erb
< %= search_form_for @q do |f| %>
< %= f.label :name_cont %>
< %= f.text_field :name_cont %>
< %= f.label :articles_title_start %>
< %= f.text_field :articles_title_start %>
< %= f.submit %>
< % end %>
```
2011-06-06 21:32:03 -04:00
`cont` (contains) and `start` (starts with) are just two of the available search predicates.
2013-12-09 05:00:21 -05:00
See [Constants ](https://github.com/activerecord-hackery/ransack/blob/master/lib/ransack/constants.rb ) for a full list and the [wiki ](https://github.com/activerecord-hackery/ransack/wiki/Basic-Searching ) for more description.
2012-03-29 10:50:36 -04:00
2011-06-06 21:32:03 -04:00
### 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 that put a lot of power in the hands of
your users. 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. :(
This means you'll need to tweak your routes...
2013-05-04 01:13:46 -04:00
```ruby
resources :people do
collection do
2013-08-06 13:47:59 -04:00
match 'search' => 'people#search', via: [:get, :post], as: :search
2013-05-04 01:13:46 -04:00
end
end
```
2011-06-06 21:32:03 -04:00
... and add another controller action ...
2013-05-04 01:13:46 -04:00
```ruby
def search
index
render :index
end
```
2012-03-29 10:50:36 -04:00
2011-06-06 21:32:03 -04:00
... and update your `search_form_for` line in the view ...
2013-05-04 01:13:46 -04:00
```erb
2013-08-06 13:47:59 -04:00
< %= search_form_for @q , url: search_people_path,
html: { method: :post } do |f| %>
2013-05-04 01:13:46 -04:00
```
2011-06-06 21:32:03 -04:00
Once you've done so, you can make use of the helpers in Ransack::Helpers::FormBuilder to
2011-06-11 14:22:41 -04:00
construct much more complex search forms, such as the one on the
2013-12-18 14:45:49 -05:00
[demo page ](http://ransack-demo.heroku.com ) (source code [here ](https://github.com/activerecord-hackery/ransack_demo )).
2011-06-06 21:32:03 -04:00
2012-10-06 17:14:47 -04:00
### has_many and belongs_to associations
You can easily use Ransack to search in associated objects.
Given you have these associations ...
2013-05-04 01:13:46 -04:00
```ruby
class Employee < ActiveRecord::Base
belongs_to :supervisor
2012-10-06 17:14:47 -04:00
2013-05-04 01:13:46 -04:00
# has attribute last_name:string
end
2012-10-06 17:14:47 -04:00
2013-05-04 01:13:46 -04:00
class Department < ActiveRecord::Base
has_many :supervisors
2012-10-06 17:14:47 -04:00
2013-05-04 01:13:46 -04:00
# has attribute title:string
end
2012-10-06 17:14:47 -04:00
2013-05-04 01:13:46 -04:00
class Supervisor < ActiveRecord::Base
belongs_to :department
has_many :employees
2012-10-06 17:14:47 -04:00
2013-05-04 01:13:46 -04:00
# has attribute last_name:string
end
```
2012-10-06 17:14:47 -04:00
... and a controller ...
2013-05-04 01:13:46 -04:00
```ruby
class SupervisorsController < ApplicationController
def index
@search = Supervisor.search(params[:q])
2013-08-06 13:47:59 -04:00
@supervisors = @search .result(distinct: true)
2013-05-04 01:13:46 -04:00
end
end
```
2012-10-06 17:14:47 -04:00
... you might set up your form like this ...
2013-05-04 01:13:46 -04:00
```erb
< %= search_form_for @search do |f| %>
< %= f.label :last_name_cont %>
< %= f.text_field :last_name_cont %>
2012-10-06 17:14:47 -04:00
2013-05-04 01:13:46 -04:00
< %= f.label :department_title_cont %>
< %= f.text_field :department_title_cont %>
2012-10-06 17:14:47 -04:00
2013-05-04 01:13:46 -04:00
< %= f.label :employees_last_name_cont %>
< %= f.text_field :employees_last_name_cont %>
2012-10-06 17:14:47 -04:00
2013-05-04 01:13:46 -04:00
< %= f.submit "search" %>
< % end %>
```
2012-10-06 17:14:47 -04:00
2013-10-07 07:47:52 -04:00
## I18n
Take a look at our locale file on ``lib/ransack/locale/en.yml`` to check all available messages. You may also be interested in one of the many translations that are available on:
http://www.localeapp.com/projects/2999
2012-10-06 17:14:47 -04:00
2011-06-06 21:32:03 -04:00
## Contributions
2012-03-29 10:50:36 -04:00
To support the project:
2011-06-06 21:32:03 -04:00
2012-03-29 10:50:36 -04:00
* Use Ransack in your apps, and let us know if you encounter anything that's broken or missing.
2011-06-06 21:32:03 -04:00
A failing spec is awesome. A pull request is even better!
* Spread the word on Twitter, Facebook, and elsewhere if Ransack's been useful to you. The more
people who are using the project, the quicker we can find and fix bugs!
## Copyright
2012-03-29 10:50:36 -04:00
Copyright © 2011 [Ernie Miller ](http://twitter.com/erniemiller )