Tags: soulmate

ActiveRecord like where for array of hashes in ruby

Edit : Updated the code as per the suggestions made by Masklinn.

Recently I was working on Issue#1 of my Soulmate Rails gem, which is a very simple and obvious optimization that I should have thought of myself. Thanks to lephyrius though for pointing it out. In my search method I was looping through the search results and calling find on each to get it from db. The fix involved to instead use a single where for all ids so that it translates to a single db call. A simple fix as well as an optimization.

However once I implemented it, I notice my tests were failing. On closer inspection i found they are failing because the models I created for testing using supermodel gem, didn’t have a where method. For a moment I thought WTF, and then I headed on to supermodel and looked through the source and to my surprise, it in fact did not implement a where method. So I took it upon myself to do it.

Here’s the generic code I wrote that could be applied to any array of hashes :

Now you can do

And you will get what you’d expect.

I updated supermodel in my fork to support such a where method and have sent the original author a pull request, hopefully it will get pulled soon.

All my tests pass now! Yay! I have released v0.3.0 of Soulmate Rails. Be sure to check it out.

PS : How about a code review ? Is this optimal or can it be improved further ? I’d like to hear thoughts from others.

Soulmate Rails

One of the most common things to do in any standard rails web applications is auto-completion. One interesting solution to this is by using redis’ (which is blazing fast) sorted sets, storing key value pairs for partial matches. For example, if we wish to index the word ‘Dhruva Sagar’ (my name), you can simply create key -> value pairs of ‘d’ -> ‘Dhruva Sagar’, ‘dh’ -> ‘Dhruva Sagar’, ‘dhr’ -> ‘Dhruva Sagar’ and so on. Since hash lookups are of constant time complexity O(1), they’re very fast and hence the completions are too! Combine this with using sorted sets of redis to easily order / sort information on a specific criteria and you have a blazing fast solution. Of course the actual implementation can be further optimized by means of caching query results and make the system perform even further.

Soulmate is a cool gem by the guys at SeatGeek that provides a pretty cool implementation of the same. However it’s implementation wasn’t quite a good fit for my case since I wanted to work with a rails application and didn’t want to either host a separate server or mount the provided sinatra app inside of rails. I came across this nice post by Pat Shaughnessy – Finding your soulmate: autocomplete with Redis in Rails 3.1 which showcases how one can use the Soulmate gem and use it’s API to roll out your own auto-completion engine within your rails 3.1 application very easily.

This inspired me to fork Soulmate and extend it to build a Soulmate Rails gem over the weekend which provides a clean and intuitive API to do this from within Rails. Please do check it out, star it, fork it and use it to your hearts content :) . I’ve released it in alpha phase right now but it is fully usable with a reasonable test coverage. I’ll be contributing a lot to this, hopefully you find it useful too.

UPDATE : I released the first stable version 0.2.1, it should be usable as is without any issues.