Wednesday, 28 August 2013

Searching multiple columns in a text_field_tag

Searching multiple columns in a text_field_tag

3.5 and ruby 1.8.7 I'm trying to create a multiple search working with 4
columns (name,name2,last_name,last_name2), want to search in a text box my
4 columns but i don't want to create 24 conditions, is there any other way
to do this or maybe creating a conditional FOR instead of 24 conditions
I want to search by
name and name2
name and name2 and last_name
name and last_name
last_name and last_name2
...
There are 24 conditions
Actually my controller is working but i don't want to write 24 conditions
, here is my problem
**Here is my model**
class Project < ActiveRecord::Base
end
*************Here is my controller****************
class ProjectsController < ApplicationController
def index
@projects = Project.find(:all, :conditions => "(
name LIKE \"#{params[:query]}%\" OR
name2 LIKE \"#{params[:query]}%\" OR
last_name LIKE \"#{params[:query]}%\" OR
last_name2 LIKE \"#{params[:query]}%\" OR
(concat(name, \" \", last_name) LIKE \"#{params[:query]}%\")OR
(concat(name, \" \", name2 ) LIKE \"#{params[:query]}%\")OR
(concat(last_name, \" \", last_name2) LIKE \"#{params[:query]}%\")OR
(concat(name, \" \", name2, \" \",last_name, \" \",last_name2) LIKE
\"#{params[:query]}%\")OR
(concat(name, \" \", name2, \" \",last_name) LIKE
\"#{params[:query]}%\")) ")
end
end
Here is search form
*********************Here is my view
<div id="search-area">
<div id="searchbox">
<form name="search-form" id="search-form">
<label for="query"><%= ('BUSCAR') %>
<%= image_tag("loader.gif",
:align => "absmiddle",
:border => 0,
:id => "loader",
:style =>"display: none;" ) %>
</label>
<%= text_field_tag "query", params[:query], :autocomplete => 'on' %>
</form>
</div>
</div>
Someone can help me to convert this in a conditional for? or maybe other
way to find 4 columns in a text_field_tag

No comments:

Post a Comment