@@ -41,27 +41,14 @@ gem 'ransack', github: 'activerecord-hackery/ransack'
4141
4242## Usage
4343
44- Ransack can be used in one of two modes, simple or advanced.
44+ Ransack can be used in one of two modes, simple or advanced. For
45+ searching/filtering not requiring complex boolean logic, Ransack's simple
46+ mode should meet your needs.
4547
46- ### Simple Mode
47-
48- This mode works much like MetaSearch, for those of you who are familiar with
49- it, and requires very little setup effort.
50-
51- If you're coming from MetaSearch, things to note:
48+ If you're coming from MetaSearch (Ransack's predecessor), refer to the
49+ [ Updating From MetaSearch] ( #updating-from-metasearch ) section
5250
53- 1 . The default param key for search params is now ` :q ` , instead of ` :search ` .
54- This is primarily to shorten query strings, though advanced queries (below)
55- will still run afoul of URL length limits in most browsers and require a
56- switch to HTTP POST requests. This key is [ configurable] ( https://github.com/activerecord-hackery/ransack/wiki/Configuration ) .
57-
58- 2 . ` form_for ` is now ` search_form_for ` , and validates that a Ransack::Search
59- object is passed to it.
60-
61- 3 . Common ActiveRecord::Relation methods are no longer delegated by the
62- search object. Instead, you will get your search results (an
63- ActiveRecord::Relation in the case of the ActiveRecord adapter) via a call to
64- ` Ransack#result ` .
51+ ### Simple Mode
6552
6653#### In your controller
6754
@@ -84,6 +71,20 @@ def index
8471end
8572```
8673
74+ ##### Default search parameter
75+
76+ Ransack uses a default ` :q ` param key for search params. This may be changed by
77+ setting the ` search_key ` option in a Ransack initializer file (typically
78+ ` config/initializers/ransack.rb ` ):
79+
80+ ```
81+ Ransack.configure do |c|
82+ # Change default search parameter key name.
83+ # Default key name is :q
84+ c.search_key = :query
85+ end
86+ ```
87+
8788#### In your view
8889
8990The two primary Ransack view helpers are ` search_form_for ` and ` sort_link ` ,
@@ -670,6 +671,43 @@ Trying it out in `rails console`:
670671
671672That's it! Now you know how to whitelist/blacklist various elements in Ransack.
672673
674+ ### Handling unknown predicates or attributes
675+
676+ By default, Ransack will ignore any unknown predicates or attributes:
677+
678+ ``` ruby
679+ Article .ransack(unknown_attr_eq: ' Ernie' ).result.to_sql
680+ => SELECT " articles" .* FROM " articles"
681+ ```
682+
683+ Ransack may be configured to raise an error if passed an unknown predicate or
684+ attributes, by setting the ` ignore_unknown_conditions ` option to ` false ` in your
685+ Ransack initializer file at ` config/initializers/ransack.rb ` :
686+
687+ ``` ruby
688+ Ransack .configure do |c |
689+ # Raise errors if a query contains an unknown predicate or attribute.
690+ # Default is true (do not raise error on unknown conditions).
691+ c.ignore_unknown_conditions = false
692+ end
693+ ```
694+
695+ ``` ruby
696+ Article .ransack(unknown_attr_eq: ' Ernie' )
697+ # ArgumentError (Invalid search term unknown_attr_eq)
698+ ```
699+
700+ As an alternative to setting a global configuration option, the ` .ransack! `
701+ class method also raises an error if passed an unknown condition:
702+
703+ ``` ruby
704+ Article .ransack!(unknown_attr_eq: ' Ernie' )
705+ # ArgumentError: Invalid search term unknown_attr_eq
706+ ```
707+
708+ This is equivilent to the ` ignore_unknown_conditions ` configuration option,
709+ except it may be applied on a case-by-case basis.
710+
673711### Using Scopes/Class Methods
674712
675713Continuing on from the preceding section, searching by scopes requires defining
866904 title: Old Ransack Namespaced Title
867905` ` `
868906
907+ # ## Updating From MetaSearch
908+
909+ Ransack works much like MetaSearch, for those of you who are familiar with
910+ it, and requires very little setup effort.
911+
912+ If you're coming from MetaSearch, things to note :
913+
914+ 1. The default param key for search params is now `:q`, instead of `:search`.
915+ This is primarily to shorten query strings, though advanced queries (below)
916+ will still run afoul of URL length limits in most browsers and require a
917+ switch to HTTP POST requests. This key is
918+ [configurable](default-search-parameter) via setting the `search_key` option
919+ in your Ransack intitializer file.
920+
921+ 2. `form_for` is now `search_form_for`, and validates that a Ransack::Search
922+ object is passed to it.
923+
924+ 3. Common ActiveRecord::Relation methods are no longer delegated by the
925+ search object. Instead, you will get your search results (an
926+ ActiveRecord::Relation in the case of the ActiveRecord adapter) via a call to
927+ ` Ransack#result` .
928+
869929# # Mongoid
870930
871931Mongoid support has been moved to its own gem at [ransack-mongoid](https://github.com/activerecord-hackery/ransack-mongoid).
0 commit comments