I currently have these 3 search methods in my rails model, they are all
the same apart from which field they search (take a look at the 5th line
inside each method).
def self.search(*args)
return [] if args.blank?
cond_text, cond_values = [], []
args.each do |str|
next if str.blank?
cond_text << “( %s )” % str.split.map{|w| "game_name LIKE ? “}.join(”
OR “)
cond_values.concat(str.split.map{|w| “%#{w}%”})
end
all :conditions => [cond_text.join(” AND "), *cond_values]
end
def self.gensearch(*args)
return [] if args.blank?
cond_text, cond_values = [], []
args.each do |str|
next if str.blank?
cond_text << “( %s )” % str.split.map{|w| "genre LIKE ? “}.join(” OR
“)
cond_values.concat(str.split.map{|w| “%#{w}%”})
end
all :conditions => [cond_text.join(” AND "), *cond_values]
end
def self.consearch(*args)
return [] if args.blank?
cond_text, cond_values = [], []
args.each do |str|
next if str.blank?
cond_text << “( %s )” % str.split.map{|w| "console LIKE ? “}.join(” OR
“)
cond_values.concat(str.split.map{|w| “%#{w}%”})
end
all :conditions => [cond_text.join(” AND "), *cond_values]
end
I currently have the following in my controller:
def index
@games = Game.search(params[:search])
@games = Game.gensearch(params[:search])
@games = Game.consearch(params[:search])
end
and the following in my view:
<%= form_tag games_path, :controller => ‘games’, :action => ‘search’,
:method => ‘get’ do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag t(’.searchb’), :game_name => nil %>
<% end %>
<%= form_tag games_path, :controller => ‘games’, :action =>
‘gensearch’, :method => ‘get’ do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag t(’.searchb’), :game_name => nil %>
<% end %>
<%= form_tag games_path, :controller => ‘games’, :action =>
‘consearch’, :method => ‘get’ do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag t(’.searchb’), :game_name => nil %>
<% end %>
I am having a problem that no matter what form I type text in to, it
searches all the forms and displays the text in each form as if they are
all connected. It will only return results that match all three columns
as well despite me stating what action I want each to link_to.
Any ideas on what I should change?
Thanks.