Search of multiple columns

I am currently writing a search method for my rails applications and at
the moment it works fine. I have the following in my game.rb:

def self.search(search)
  if search
    find(:all, :conditions => ['name LIKE ? OR genre LIKE ? OR
console LIKE ?', "%#{search}%", "#{search}", "#{search}"])
  else
    find(:all)
  end
end

Now that searches fine but my problem is that if there is a record in
game_name that has the words playstation in it will finish the search
there and only return that record rather than that as well as all games
that have playstation stored in console. Now I understand that is
because I have OR in my conditions but I don't know an alternative. AND
simply requires all the conditions to match or no return at all. What is
an alternative I can use to AND and OR. Help would be much appreciated.

If there is a solution that has seperate search boxes and entries then
that would be fine, I don't necessarily require the search to find it
all based on one search form.

IF you really need to search over several columns, use concat() for db columns... like

scope :search, labda{|search| where("concat(name, genre, console) like ?", "%#{search}%").presence || all }

and then use

Model.search('value')

tom

Hey I found out my problem, it was because I did not have the % symbol
in the last two search calls, all works fine now.

Do you know how I would be able to make it as three separate search
boxes and buttons instead of all going in to one?

All I know is how to search every column under one form but would like
to do it as three separate forms. Not like in the advance search by
railscast but three text fields with three submit buttons.

Thanks
R. Patrick

Hey I found out my problem, it was because I did not have the % symbol
in the last two search calls, all works fine now.

Do you know how I would be able to make it as three separate search
boxes and buttons instead of all going in to one?

All I know is how to search every column under one form but would like
to do it as three separate forms. Not like in the advance search by
railscast but three text fields with three submit buttons.

Thanks
R. Patrick

Take a serious look at ransack. It's a very neat search gem, and it allows you to create specialized search fields just by naming those fields according to a dsl. So your controller would look like this:

@q = ModelName.search(params[:q])
@model_names = @q.result(:distinct => true)

No changes needed no matter what sort of hijinks you get up to in your view:

<%= search_form_for @q do |f| %>
<%= f.search_field :foo_cont, :class => 'search' %> (foo contains the search parameter)
<%= f.search_field :bar_start, :class => 'search' %> (bar starts with the search parameter)
<%= f.submit :name => nil, :value=> 'Search' %>
<% end %>

It contains the following predicates, which you just tail on to the name of the field you wish to search:

eq
not_eq
matches
does_not_match
lt
lteq
gt
gteq
in
not_in
cont
not_cont
start
not_start
end
not_end
true
false
present
blank
null
not_null

And if that's not enough, you can create your own very directly.

Walter

Take a serious look at ransack. It's a very neat search gem, and it
allows you to create specialized search fields just by naming those

It sounds really good Walter, do you have any links for Ransack, I just
googles ransack - rails but it hasn't provided me with a documentation
link.

Thanks
Patrick

https://github.com/ernie/ransack

Best regards

Peter De Berdt

Hey I installed the ransack gem and have added some basic code to my
code but have received an error now on my page which is the followin

undefined method `result' for #<Array:0x44cd540>

I have the following code in my gamescontroller.rb

  def index
     @q = Game.search(params[:q])
     @game = @q.result(:distinct => true)
  end

and the following code in my games/index.html.erb

<%= search_form_for @q do |f| %>

  <%= f.label :game_name_start %>
  <%= f.text_field :game_name_start %>
  <%= f.submit %>
<% end %>

any ideas of what the problem might be?

Hey I installed the ransack gem and have added some basic code to my
code but have received an error now on my page which is the followin

undefined method `result' for #<Array:0x44cd540>

I have the following code in my gamescontroller.rb

def index
    @q = Game.search(params[:q])
    @game = @q.result(:distinct => true)
end

and the following code in my games/index.html.erb

<%= search_form_for @q do |f| %>

<%= f.label :game_name_start %>
<%= f.text_field :game_name_start %>
<%= f.submit %>
<% end %>

any ideas of what the problem might be?

Didn't you have a search method that you wrote earlier? That may be overriding the one in the Ransack gem. Ransack returns an Arel collection, not an array.

Walter

Walter Davis wrote in post #1048250:

Walter Davis wrote in post #1048250:

end

any ideas of what the problem might be?

Didn't you have a search method that you wrote earlier? That may be
overriding the one in the Ransack gem. Ransack returns an Arel
collection, not an array.

Walter

I still have a method in the model but I have removed the old search
from the index.html.erb. So I can't see that being the problem.

Try removing or renaming the search method in the model. I am guessing that that is clobbering the search method that Ransack is injecting into your model by default.

Walter