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?
Like you've done above, but I tend to usee:
["surname LIKE ?", "%{params[:query]}%"]
Is these a case to be made for adding "like" support to rails?
Not for me, but then again, I'm not everybody One potential problem is once you add like someone is going to want regex (ie. the ~ operator in postgres for example) and they'll probably also want "NOT LIKE" and then there's the whole issue of case [in]sensitivity between databases...
I'm just saying that there's a lot of options and they tend to get somewhat database specific so maybe it's a hard thing to generalize.
Also, if you did to "like", I'd also like "begins_with" that left off the initial '%'... and so on
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")
How are you going to distinguish these methods operating on a model with a column "surname" vs a column "surname_like". Granted that's an odd name for a table column, but it could happen.
Member.find(:like => {�Surname� => "cla"})
This seems so close to just using the :conditions I don't see the point...
Personally I prefer the first one. Josh Susser has a good article on how dynamic finders work (has_many :through - 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?
For me, no, but don't let me stop you
-philip