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
What I want to do now is declare these three in the controller and then display them on the application page. It works for one but my problem is that I don't know what variables to put where for the other two. I learnt the first one from a basic search. I would like to keep them as three separate searches.
I currently have the following in my controller:
@games = Game.search(params[:search])
@games = Game.gensearch(params[:search])
@games = Game.consearch(params[:search])
and the following in my view:
<div id="sidehead"><h2><%= t('.search') %></h2></div>
<div id="searching" >
<%= 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 %>
</div>
<div style="clear: both;"/>
<div id="sidehead"><h2><%= t('.search') %></h2></div>
<div id="searching" >
<%= 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 %>
</div>
<div style="clear: both;"/>
<div id="sidehead"><h2><%= t('.search') %></h2></div>
<div id="searching" >
<%= 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 %>
</div>
I want to take the last two parts (controller, view) and do them three times, one for each of the search methods but with the appropriate variables, any assitance would be of help and would give further knowledge.
Thanks.