Hi all,
Last night a friend and I decided to try and implement a search feature
for a website (one we are using to learn Rails). We have a membership
database and thought it would be good to search for members based on
their surname (starting easy and working up) - this was pretty easy:
Member.find_all_by_surname(params[:query])
OK - What if you don’t know the exact surname, or what a list of every
member with “cla” in their surname?
Our solution late last night was:
Member.find(:all, :conditions => [“surname LIKE ?”, “%” +
params[:query] + “%”])
It works but it does not seem very “rails like”, so this morning I went
looking for a :like or find_by_surname_like - it seems none exist.
So my question is this - how do other people implement these kind of
searches?
Is these a case to be made for adding “like” support to rails?
If so - what should it look like? Here are some ideas:
Member.find_by_surname_like(“cla”)
Member.find_all_by_surname_like(“cla”)
Member.find_surname_like(“cla”)
Member.find_like_surname(“cla”)
Member.find(:like => {“Surname“ => “cla”})
Personally I prefer the first one. Josh S. has a good article on
how dynamic finders work
(http://blog.hasmanythrough.com/2006/08/13/how-dynamic-finders-work), so
this would make an interesting project for me to get my hands dirty in
Rails code. I also notice a proposed fulltext search on the wiki
(http://wiki.rubyonrails.org/rails/pages/FullTextSearch).
But before I have a go at this - how do you think something like this
should work?