Cristian Livadaru's blog

... think again ...

Creating a Ruby on Rails Search With Squirrel

I was trying to create a search form with all kind of search fields but almost no text fields so Ferret is nothing that would help me since I need no full text search.
I found several hints but none was really what I was looking for until I came across something on scribd (http://www.scribd.com/doc/3188387/Advanced-Searching-in-Rails) which didn’t help me either but it gave me the link to Squirrel which seemed to be exactly what I needed.
But as usual there is something that doesn’t quite work as expected, in my case the first problem was with checkboxes. What I tried to accomplish is that it should return all records that have true in the field if the checkbox is checked and ALL records (also the ones with false) if the checkbox is not checked.
My problem was that I was trying all kind of comparisons with ==true or ==1 which all failed. The correct way to do it is ==”1″ for true or ==”0″ for false. Here an example:

1
2
3
@course= Course.find(:all) do
kinderbetreuung==true unless params[:course_search][:kinderbetreuung]=="0"
end

The next problem I had is that I have for each day separate times for the course, yeah I know this is a bit to much work but I really had no better Idea how to solve it. Anyway, my problem was that a course could be on just one day of the week or 3 times a week. This makes the search a bit more complicated.
Let’s say you are searching all courses that start after 09:00. The correct way to do it is like this:

1
2
3
4
any do
kurszeit_mo_von >= zeit_von.to_s
kurszeit_mo_von==nil
end

This will return either if the coursetime for monday matches OR if the coursetime is null. Hope I could save someone a lot of time with this.