Ransack, search multiple columns, one field, rails 3
Asked Answered
E

1

11

I'm new to Ruby on Rails, and I'm creating my first site. I have a user model with 3 columns: firstname, lastname and email. Im using ransack to make a search engine, and i want to have one search field that will go through every column and display the result.

I have made a user_controller.rb looking like this:

@q = User.search(params[:q])
@users = @q.result(:distinct => true)

And my User\index.html.erb looks like this:

<%= search_form_for @search do |f| %>
  <div class="field">
    <%= f.label :firstname_cont , "Firstname" %>
    <%= f.text_field :firstname_cont %>
    <%= f.label :lastname_cont , "Lastname" %>
    <%= f.text_field :lastname_cont %>
    <%= f.label :email_cont , "Email" %>
    <%= f.text_field :email_cont %>
  </div>
  <div class="actions"><%= f.submit "Search" %></div>
<% end %>

Giving me 3 search fields, but i only want one that goes through firstname, lastname and email.

Edit 1 : After testing some more, I found out that the search field can't handle both firstname AND lastname, or any whitespaces. I have read that it is possible to make a helper_method like this: Searching multiple fields with Ransack

And when I get the search result I get thrown into my model index etc: user / index. Where can I change this?

But there is nothing on how to use it in the view. Why is it so hard to just whrigt, this is what you do :in Controller do this -> in model do this -> in view do this -> And dont forget to fixs the routes like this ->

Embrey answered 14/3, 2013 at 13:24 Comment(0)
K
25

First of all, before asking try to search a bit through the gem docs and googling a bit ^_^.

The first one to refer is the github page : https://github.com/ernie/ransack

Looking at the demo page of Ransack here : http://ransack-demo.herokuapp.com/ you should see the first field does what you want (search in both in lastname and firsname) and having a look at the source code of the page you should see this line

<input id="q_first_name_or_last_name_cont" name="q[first_name_or_last_name_cont]" size="30" type="text" />

which gives you an idea on the correct predicate to use, you can also have a list of them here : https://github.com/ernie/ransack/blob/master/lib/ransack/constants.rb (always in the gem doc on github).

Based on that you can do something like that in your view

<%= f.text_field :lastname_or_first_name_or_email_cont, :placeholder => 'full name or email contains' %>

that generates a 'OR condition' query, as you can see in the demo page again (where the query generated is dispayed as a traditional SQL query)

Hope this helps

Cheers

Konopka answered 16/3, 2013 at 11:26 Comment(6)
Can i ask you for some more help? I have noticed that whitespace, or searching with more then 1 word will make this method to fail. Like if firstname is Foo, and lastname is Bar, and i search Foo bar, it will fail.. Is it possible to build a : def set_full_name .... end, and then call it in the view? and how do you call it in the view? with helper_method? i dident manage to do this.. And one last thing, if i have search any other place, my results will allways throw me to the model - index. Why is this?Embrey
Hi Remi, I'm facing the same wall about searching with multiple words in the search field and did not find any answer at the moment. You can probably use a ransacker to build a 'virtual column' to search on, there's an example here github.com/ernie/ransack_demo/blob/master/app/models/user.rb. 2nd part of your question, the results always throws you to the model-index because in the 'search' action of your controller you render the 'index' view. CheersKonopka
Nice, thank you! I will take a closer look at that demo. Im still learing RoR on my own, and trawling the web for information.Embrey
what if i want to search for last_name(string) or id(integer)?Aloysia
@Aloysia - why would you want to search for ID? From a user perspective ID should normally be kept invisible....Perceivable
BTW, is it possible to say that you want the results sorted in some kind of relevance? Exmaple: in this case, first the results that match the "firstname" and then, "lastname"?Warnke

© 2022 - 2024 — McMap. All rights reserved.