Blog

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…

Materialized View Alternatives

A while back I wrote about materialized views in Postgres. My main motivation was making Postgres’ full-text search faster, and to be able to filter and sort using indexes when searching over data coming from many different tables. Since then, I’ve learned about some alternatives that I want to share. First, a note about why materialized views are not always the right tool for the job: 1. When creating materialized views, you end writing SQL that duplicates logic you’ve already implemented in Ruby, like this: In that example, you have to write two versions of how to find and format…

Slim for static sites

I’m a big fan of preprocessing. I prefer Slim, Sass, and CoffeeScript to plain HTML, CSS, and Javascript. They are cleaner to read and easier to type. They are also very easy to use in Rails. But what about using them for static sites, without Rails? Although Sass and CoffeeScript have command line compilers, I couldn’t find anything as easy to use for Slim. In the end, I realized that the right way to go is using Middleman, a Ruby framework for building static sites and blogs. It uses the same sort of asset pipeline as Rails does, so all of the compiling…

Fixing font-awesome on Heroku

My icons from the font-awesome-rails gem were showing up fine in my development environment. But in production on Heroku, some showed up as empty boxes, and others did not show up at all. In the “Network” tab of Google Chrome, I saw that the http response status for the font assets was: 206 Partial Content. Not very informative. A more useful message came from the “Console” tab: Font from origin http://shielded-falls-2406.herokuapp.com has been blocked from loading by Cross-Origin Resource Sharing policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin http://expensemanager.teddywidom.com is therefore not allowed access. I had recently…