Reputation: 461
I'm following Railscast: http://railscasts.com/episodes/37-simple-search-form?autoplay=true
The result shows well in my implementation. But because I have first_name and last_name columns instead of just "name" as in the example above, I need a way to search for both no matter what the search term is. How can I do this? Right now only first name search works.
user.rb
def self.search(search)
if search
where('first_name LIKE :search OR last_name LIKE :search', search: "%#{search}%")
else
all
end
end
people_controller, index function:
@users = User.search(params[:search])
index.html.erb (for people controller):
<%= form_tag people_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag("Search users", first_name: nil, last_name: nil) %>
</p>
<% end %>
<ul>
<% @users.each do |user| %>
<li>
<%= link_to profiles_path(user_id: user.id) do %>
<%= user.first_name %>
<%= user.last_name %>
<% end %>
</li>
<% end %>
</ul>
UPDATE:
So this is weird. I have 4 users. For the first 3, only first name search works. Last name or full name (or partial name) doesn't. For the last user, only last name search works.
...what could be going on? Just made three more users and tested. The first new user got searched only by first name. The second new user got searched by only last name. His first name is part of the last name of 3rd user, and that 3rd original user name was returned when searched by that new user's first name (...why? didn't work like this before; partial name search doesn't work for other users). And third user's last name only was searched.
UPDATE:
OK, didn't do anything. But now the first original's user gets searched by either last name or first name (only first name before). WHY? Second original user as well. And partial search for two users works now. WHY? And the last new user whose last name (only) was searchable is now not searchable by any. WHY?
And tried it again just now and returned to previous, searchable by first name only, no partial search, etc. Why is this keep changing? How can I make the search term simply search for either first name or last name or both?
UPDATE:
I got the answer (to the above, not in general). The search is CASE-SENSITIVE. Now my question is: how can I make it non-case-sensitive and have the search term that includes both first name and last name (eg "Tom Paulson") work? (right now only searching for either works)
UPDATE:
So I solved case sensitivity by changing LIKE to ILIKE. I'm using Postgres.
If you could help with searching for both first name and last name, I'd appreciate it!
UPDATE:
So I'm now using this to search for both or either first name and last name. But now partial search does not work. Is there a way to make that work here as well?
where("first_name ilike :search or last_name ilike :search or first_name || ' ' || last_name ilike :search", search: "%#{search}")
UPDATE:
Solved it by adding % to %#{search}
%#{search}%
The case seems resolved. Thank you! I will maybe post an answer later.
Upvotes: 0
Views: 417
Reputation: 1333
UPDATE:
ooh, you wanted to search in both columns.
one way is to use 'concat'.
assuming you put a space in between first and last name:
where("CONCAT(first_name, ' ', last_name ) ILIKE ?","%#{search}%")
FIRST ANSWER:
def self.search(search)
if search
buff=search.downcase
where('lower(first_name) LIKE ? OR lower(last_name) LIKE ?', "%#{buff}%", "%#{buff}%")
should be working.
or ILIKE if you prefer.
where('first_name ILIKE ? OR last_name ILIKE ?', "%#{search}%", "%#{search}%")
can you check the value of 'params[:search]' if it is not nil?
Upvotes: 0