EDIT: A lot of people asked for a plugin of this library. You can find it on GitHub.
Part of my current project screamed for some basic searching. In typical Ruby development fashion, the first thing I think of is “someone else must have done this already.” Sure enough, there were several, but the one the fit the best for me was the query version found here. It looked perfect, but there were a few problems.
- The Wiki messed up a lot of characters, making it difficult to get the code.
- A few things didn’t work quite right
Time to fix the problems. First, I needed to get the code onto my disk without spending hours finding all the places where the Wiki messed things up. A quick Google search for “search apply_demorgans” located what I was looking for. Now I had something that basically worked. Unfortunately, a few items did not work as advertised.
The first problem I found was that if you added a searches_on to your model object, you could not override the searchable fields. A quick update to self.searchable_fields solved that problem. The second problem was that if you included another table in your search, the searchable fields were not automatically pulled in as advertised. In addition, searchable fields did not include the table name so any searches with an include didn’t always work (need unique column names). Finally, I updated the search routine to also return a count so you can more easily paginate the results.
That’s it for now. The remaining issue I have is that include only works if the association variable name is the same as the table name. However, if you have an association with a different name or finder_sql, it doesn’t work. I don’t have a huge need for this right now, so I haven’t fixed it yet.
Anyone out there care to chime in?
Here is my final code: [REMOVED] Get the plugin from GitHub.
Leave a Reply