Tutorial: Caching virtual attributes in the database

Ever since I released Searchlogic I’ve been getting a lot of really cool feedback. I love when people contact me with a unique way they used Searchlogic or a challenging / cool new feature they would like to see added. Challenges are what make programming fun, to see what you are capable of. So here is my challenge for the day:

I’ve been having a lot of conversations via email with people using Searchlogic, that go like the following:

Some person: Hi Ben, How do I order by an order’s total with Searchlogic?

Me: Cache the total in your database. Your idol, Ben

Some person: Before Searchlogic, I was just doing: Order.all(:order => “(quantity * price) ASC”). Is there a way to do this in Searchlogic?

Me: No, you shouldn’t order data like that. …long explanation of what to do…. Your idol, Ben

Some person: Thank you so much Ben, I can’t thank you enough for everything you have done for me. You truly are my idol. There should be a statue in your honor.

Maybe it didn’t go exactly like that, but you get the point. So instead of having to retype the “long explanation of what to do”, I can just link them to this post:

This can be simple or complicated depending on how you calculate your total. Let’s start with the simple:

(more…)