Posts

Showing posts with the label Mobility gem

Eliminate n+1 Calls When Using the Mobility Gem To Retrievie All Records Of a Model

The Mobility Gem is a a gem for storing and retrieving translations as attributes on a class. Instead of requiring multiple migrations for every attribute that needed to be translated, Mobility uses only two shared tables to store all translated attributes (This is my understanding of it, if I am wrong, please correct me.) While Mobility provides a handy method for querying such that only 1 sql call is made to find the corresponding record with the queried translated attribute. A problem exists when I tried to retrieve all records of a class and display each of their translated values - doing so, would cause n+1 calls to the database, each record makes a separate call to retrieve the translated value of the attribute. I thought of 2 ways to solve this:  Caching, all the translated attributes are cached after it has been called once  A complex sql call to eliminate n+1 calls This post will focus on the second method. ---- I've written a helper method to retrieve eit...