Working Through RailsCasts - Picking Tasty Tidbits
I'm slowly working my way through all of the Railscasts, with the goal of being able to understand more of what the team is up to, and maybe suggest some changes.
Thanks to a suggestion by my friend Luke Melia, I'm going to identify my favorite tips and tidbits, and share my thoughts about why they're my favorites. This forces me to tread carefully the line between "I'm a techie/programmer" and "I'm a manager of an agile team". Hopefully, I'll manage okay.
I'm delighted to say that my first favorite tidbit comes from the first Railscast from March, 2007 titled "Caching with Instance Variables". I should mention that the Railscasts are the work of Ryan Bates, who is a Rails Core Contributor, among other claims to fame.
In the first Railscast, Ryan suggests that it's more efficient to cache user information in the Application Controller rather than pulling it out of the database every time you need it.
This rang a bell for me, since we have a nifty current_user helper that is pulling the user info from the database every time it's invoked. While usage is not a big deal right now, while we're in development, it could become so later on. And while we may eliminate this helper later on, why not use good practices now?
There was an additional lesson in this for me - that the Application is instantiated for each session, so storing the user in a cached instance variable makes sense. It may seem obvious, but I hadn't really thought about it before, and since we hadn't used any instance variables in the Application controller, I had no examples to lead me to ask the question.
I checked in with my team, and asked if there was any reason we shouldn't use this technique. The answer? Try it and run the tests and if it passes, then it's okay. I did, they did, and it is.
Thanks, Ryan!