Paulamorphism: tales from Rails

6/2/2017

​This week was Rails week, and our remote cohort of 17 programmers from 5 countries working in 3 timezones spent a week making Acebook, a clone of — you guessed it — Facebook.

It was a great, if hard, week. My learning shot up exponentially. Also my ice cream consumption. Here is what I learned:

1. Red/refactor/red/red/red/red/red/red/red/red/red/red/red/red/red/red/red/red/red/red/red/red/red/red is not a great cycle. Red, green, and ONLY THEN refactor. We spent a lot of time looking for bugs in our code when it turned out our partial was named incorrectly and Rails couldn’t find it.

2. 
​Polymorphic (alas, not paulamorphic, much as I love eponymous taxonomy) associations are really cool. I’m glad they’re so cool, because here was the process in which my pair and I learned about them:

Step 1:
Build a PhotoComments functionality, almost wholly copying the already-existing Comments functionality. This was not remotely DRY, but we couldn’t figure out how to get round the database issues. This took a whole day because of the aforementioned red/refactor/red/red/red/red/red/red/red/red/red/red/red/red/red/red/red/red/red/red/red/red/red/red and we were super pumped to finally have it submitted for PR.

Step 2:
Our coach tells us that there has to be a dryer way and blocks our PR. His name is Dan, and he’s a great coach because he made us go and find something way better. Was I still banging my head in frustration? Yes.

Step 2.5:
Help my landlord remove the old dishwasher that’s being replaced and end up with a broken hose and water gushing out over the kitchen floor before I luckily had the presence of mind to switch off the water switch on the pipe. Not flooding the kitchen is possibly my proudest moment of the whole week, and I’m pretty proud of Rails. I did later get a bottle of prosecco as a reward for valiant behaviour, so all in all, a good week.

Step 2.75
Have standup without coffee or breakfast (see Step 2.5) and decide to throw out everything we did yesterday.

Step 2.90
Go to make coffee, forgetfully turn on the kitchen tap, and watch the water flow out everywhere. Put a tea cosy over the faucet so that doesn’t happen again.

Step 3
Google. It’s a solved problem, so look for the most obvious answer:

This first searched proved extremely productive (newer searches first):

Step 4
Use TDD to build a polymorphic database!

Step 5
Be foiled in your attempts to merge your PR because of merge conflicts that can’t be resolved in the ten minutes before retro.

Step 6
Spend another hour that night working on all kinds of git conflicts.

Step 7
Give up on resolving the merge and write a blog.

Still, polymorphism is really cool. And if you’re still awake, you might be wondering what it is! Basically, it’s a way of reusing code so that, for example, the code for comments could be used for both posts and photos. It defines the DB associations so that the comments are polymorphic, i.e. can exist with either a post_id or a photo_it. The Ruby on Rails Guide has a great section which we found really useful, and there are various other blogs with step-by-step instructions.

So I think that’s it for Rails week. I’m building an Instagram clone this weekend, so I’d better get off the computer. I can’t stand Instagram, so if you’re very lucky, you might get a grumpy post from me about how I wish I could use Rails to build something more interesting this weekend. Or about how I tried to build something more interesting and ended up melting my laptop.

Read about the first half of Rails week: Buttoning Up Code: a valiant team uses TDD, pairing, and XP values to defeat a bug

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s