0

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.

1 Answer 1

1

Easy! each search function returns an array, so you simply add the three arrays :)

@games = Game.search(params[:search]) + Game.gensearch(params[:search]) + Game.consearch(params[:search])
Sign up to request clarification or add additional context in comments.

3 Comments

Hey whistler that's great. Is there any way of doing it through three separate forms as opposed to calling them through the same form? I think I would rather it that way because this works but it is able to call through the same form and can make the results return heavy.
Oh then you simply need to create three forms and three different actions. Just change form_tag to something like this: <%= form_tag :controller => 'games', :action => 'gensearch', :method => 'get' do %> obviously games controller has an action called gensearch calling Game.gensearch and set up the route to allow the new method. Repeat the same for consearch.
Hey whistler, I have updated my code above to what I have in my sections and my problem is that no matter what text field you type in it searches that text in them all and will only return results that match all three fields. I am not too sure why :s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.