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.
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.
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:
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.
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.