Rails 4.0 to 4.1 Upgrade

When updating from Rails 4.0 to 4.1, I ran into two issues: The first was that has_and_belongs_to_many associations now seem to require a primary key on the join table. Easy enough to solve – just add a primary key to your join table. The second was that ActiveRecord #count queries have changed behaviour a bit. Before, the select clause defaulted to SELECT(*). So, even if you had something like: The query would end up in SQL as: The newer version generates the (more accurate) SQL: In my case this actually broke some places where I was implicitly relying on count(*)….

Decoupling from Rails

Here is a great talk called Decoupling from Rails by the late Jim Weirich. This talk is most famous for being taken up by David Heinemeier Hansson as an example of “test induced design damage” during his Is TDD Dead? conversation series with Kent Beck and Martin Fowler. Putting that debate aside, though, here are some things this talk got me to understand more deeply or start thinking about: Differentiating between “framework” code that we don’t own, and “application” code that we do. It’s interesting to think about defining clear boundaries between these two types of code, and about decoupling…

From Good to Great

The third great conference talk I want to share is Ben Orenstein’s Aloha Ruby Conf 2012 talk called Refactoring from Good to Great. Ben shows some common refactoring patterns by doing live coding on stage. He not only covers what he’s doing, but he also talks a lot about the why. I learned a tremendous amount from this talk. Here are some of the topics he covers: Extracting temporary variables to query Tell don’t ask Reducing parameter coupling by extracting data clumps into an object Feature envy And a few more. My favourite quotes from the talk are: I’m starting…

A therapy session with Katrina Owen

This week I’m posting a mini-series on great conference talks. Today’s installment is a talk by Katrina Owen called Therapeutic Refactoring. I’ve watched and re-watched, and re-re-watched this talk several times over the past year. Each time, it just seems to get better and better. I understand more of it, and notice and learn new things. It’s also got great slides, a lot of which are from Hyperbole and a Half. Katrina is also running exercism.io, a site where you can complete small coding problems and get peer feedback on them. I’ve been enjoying participating in that recently as well….

Sandi Metz does Magic

This week, I’ll be posting links to some great conference talks that I’ve either attended in person or seen online. Sandi Metz gave a really great talk on proper testing, called The Magic Tricks of Testing at RailsConf 2013. I can’t wait to put some of her advice into action! Here are some memorable quotes from the talk: Integration tests are proof that the beast is alive! And: This is our job… To find the simplicity that lives at the heart of complexity.

Best Practices for Loading Code

I’ve made it a whole year without thinking about best practices for loading code. I’ll blame that on the wonderful magic of Rails, for taking care of details like this for me behind the scenes. I was working on a very small Ruby program and found myself wondering if this is a code smell: To make a long story short, the answer is YES. In this example, our spec files not only know about their dependencies, they even know where those dependencies physically reside on disk in relation to themselves. Let’s refactor. I bet most if not all our spec…

Sharing HTML and PDF code with the facade pattern

In the system I wrote for my former company, several pages needed to be available in both HTML and PDF formats.  As usual, there was a tradeoff to consider: If we exported the HTML document directly to a PDF using wicked_pdf, then there was less code to write, but the resulting PDF wouldn’t look as good. If we wrote the PDF code separately from the HTML using Prawn, then the PDFs looked nicer, but then we had twice as much code to maintain. We went the latter route, and ended up with a lot of code that felt redundant: Some of this…