I'm using Ransack to allow advanced searching on my users. Currently, the users have first_name
, middle_name
, and last_name
columns. Here is my code:
.field
= f.attribute_fields do |a|
= a.attribute_select
...
How can I have a custom one called 'Name', that lets the user search through all three of the columns mentioned above?
Note I would still like to keep the remaining attributes as options as well, such as email, phone number, etc. Please keep this in mind when determining an answer.
Use this to search multiple fields:
This will generate a query like this:
when you fill in a q as search parameter
I would suggest to provide a dedicated column for this search. While that might create redundant data, it is way easier to search than doing some SQL magic on the existing columns.
You can easily automate the setting of this field:
Then you can use the normal ransack methods to search this field.
Another approach is to search every attribute of the model, excluding only the fields that you don't want to search. To do this, you could create a custom helper method that builds the label and search field name expected by Ransack's search method. The following method (located in a helper) returns a concatenation of all attributes that you wish to search in a way that Ransack expects:
Then, just add the method call to your search form: