Multiple search form error

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:

   @games = Game.search(params[:search])
   @games = Game.gensearch(params[:search])
   @games = Game.consearch(params[:search])

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.

The first thing to do is determine which bit is going wrong. Look in
development.log to make sure the correct action is being invoked and
the parameters are correct. Once that is working then use one or more
of the debug techniques described in the Rails Guide on Debugging to
work out where it is going wrong.

Colin

Hey Colin,

By using my log and typing in each of the fields the same search I get
the same results back in the log which is as followed:

Started GET "/games?utf8=%E2%9C%93&search=good&commit=Search" for
127.0.0.1 at 2012-03-13 13:48:14 +0000
  Processing by GamesController#index as HTML
  Parameters: {"utf8"=>"✓", "search"=>"good", "commit"=>"Search"}
  [1m[36mGame Load (1.0ms)[0m [1mSELECT `games`.* FROM `games` WHERE ((
game_name LIKE '%good%' ))[0m
  [1m[35mGame Load (25.0ms)[0m SELECT `games`.* FROM `games` WHERE ((
genre LIKE '%good%' ))
  [1m[36mGame Load (1.0ms)[0m [1mSELECT `games`.* FROM `games` WHERE ((
console LIKE '%good%' ))[0m
  [1m[35mUser Load (1.0ms)[0m SELECT `users`.* FROM `users` WHERE
`users`.`id` = ? LIMIT 1 [["id", 35]]
Rendered games/index.html.erb within layouts/application (138.0ms)
Completed 200 OK in 352ms (Views: 286.0ms | ActiveRecord: 58.0ms)

It searches all fields apart regardless. Does it by any chance have
something to do with my controller?

Hey Colin,

By using my log and typing in each of the fields the same search I get
the same results back in the log which is as followed:

Started GET "/games?utf8=%E2%9C%93&search=good&commit=Search" for
127.0.0.1 at 2012-03-13 13:48:14 +0000
Processing by GamesController#index as HTML
Parameters: {"utf8"=>"✓", "search"=>"good", "commit"=>"Search"}
[1m[36mGame Load (1.0ms)[0m [1mSELECT `games`.* FROM `games` WHERE ((
game_name LIKE '%good%' ))[0m
[1m[35mGame Load (25.0ms)[0m SELECT `games`.* FROM `games` WHERE ((
genre LIKE '%good%' ))
[1m[36mGame Load (1.0ms)[0m [1mSELECT `games`.* FROM `games` WHERE ((
console LIKE '%good%' ))[0m
[1m[35mUser Load (1.0ms)[0m SELECT `users`.* FROM `users` WHERE
`users`.`id` = ? LIMIT 1 [["id", 35]]
Rendered games/index.html.erb within layouts/application (138.0ms)
Completed 200 OK in 352ms (Views: 286.0ms | ActiveRecord: 58.0ms)

So does the above look correct?
Is it going to the correct action?
Are the parameters correct?
Is the correct query being run?
(I don't know the answer to those, I am just trying to help you work it out).

Colin

It is doing the correct action but doing it for all the text fields.

If for instance I type good in to the second search field (search for
genre) it will do so, but it will also produce the text in the other two
search fields and search for good in genre and console and then only
produce a results that would match all three. if I was to comment out
the other two searches and in my controller comment out two of the three
search lines it would allow me to search.

Therefore I have narrowed it down to two problems.

Either that in my controller it is taking the params[:search] as one
function or that in my view I have incorrect code that is producing text
in all fields, possibly this line as it is the same for all three:

<%= text_field_tag :search, params[:search] %>
        <%= submit_tag t('.searchb'), :game_name => nil %>

Hi Colin,

I have made some progression.

I have no changed the code a bit and the consearch works but none of the
others do despite having the same code?

My model is the same but my controller has changed to the following:

  @games = Game.gamsearch(params[:gamsearch])
  @games = Game.gensearch(params[:gensearch])
  @games = Game.consearch(params[:consearch])

and my view the following:

<%= form_tag games_path, :controller => 'games', :action => 'gamsearch',
:method => 'get' do %>
    <%= text_field_tag :gamsearch, params[:gamsearch] %>
    <%= submit_tag t('.searchb'), :game_name => nil %>
<% end %>

<%= form_tag games_path, :controller => 'games', :action => 'gensearch',
:method => 'get' do %>
    <%= text_field_tag :gensearch, params[:gensearch] %>
    <%= submit_tag t('.searchb'), :game_name => nil %>
<% end %>

<%= form_tag games_path, :controller => 'games', :action => 'consearch',
:method => 'get' do %>
    <%= text_field_tag :consearch, params[:consearch] %>
    <%= submit_tag t('.searchb'), :game_name => nil %>
<% end %>

Might it have something to do with console being the last of the list,
maybe taking in only the last set of data.

I have the following in my log for when I click consearch:

Started GET "/games?utf8=%E2%9C%93&consearch=play&commit=Search" for
127.0.0.1 at 2012-03-13 14:24:08 +0000
  Processing by GamesController#index as HTML
  Parameters: {"utf8"=>"✓", "consearch"=>"play", "commit"=>"Search"}
  [1m[36mGame Load (2.0ms)[0m [1mSELECT `games`.* FROM `games` [0m
  [1m[35mCACHE (0.0ms)[0m SELECT `games`.* FROM `games`
  [1m[36mGame Load (1.0ms)[0m [1mSELECT `games`.* FROM `games` WHERE ((
console LIKE '%play%' ))[0m
  [1m[35mUser Load (1.0ms)[0m SELECT `users`.* FROM `users` WHERE
`users`.`id` = ? LIMIT 1 [["id", 35]]
Rendered games/index.html.erb within layouts/application (242.0ms)
Completed 200 OK in 484ms (Views: 439.0ms | ActiveRecord: 39.0ms)

and the following when I click gensearch:

Started GET "/games?utf8=%E2%9C%93&gensearch=tit&commit=Search" for
127.0.0.1 at 2012-03-13 14:24:21 +0000
  Processing by GamesController#index as HTML
  Parameters: {"utf8"=>"✓", "gensearch"=>"tit", "commit"=>"Search"}
  [1m[36mGame Load (1.0ms)[0m [1mSELECT `games`.* FROM `games` [0m
  [1m[35mGame Load (2.0ms)[0m SELECT `games`.* FROM `games` WHERE ((
genre LIKE '%tit%' ))
  [1m[36mCACHE (0.0ms)[0m [1mSELECT `games`.* FROM `games` [0m
  [1m[35mUser Load (1.0ms)[0m SELECT `users`.* FROM `users` WHERE
`users`.`id` = ? LIMIT 1 [["id", 35]]
Rendered games/index.html.erb within layouts/application (252.0ms)
Completed 200 OK in 399ms (Views: 360.0ms | ActiveRecord: 34.0ms)

What I have noticed is that the CACHE and Game are swapped around in the
gensearch but I have no idea why.

All fixed now Colin, worked out the problem.