I run into a fairly common problem in rails views when I'm pulling data from multiple tables. To optmize on db access, I use :select to pre-load the data from the joined tables. For example, let's say I want to list all users with their respective cities:
Then I don't have to load the Address model to display a user's city:
The problem is that my user partial is now coupled with my named scope: if I render the partial without using the scope, I'll throw an exception. To avoid the problem, I add an accessor method to the User model that will pull the pre-loaded data if available, and revert to using the Address model:
The partial will be inefficient, but I'd prefer to find the error when profiling view times rather than through exceptions.