Blog

Recalculate counter cache columns in Rails

Rails has this great feature called counter_cache. When you have a has_many/belongs_to relationship between two models, it basically lets you say that you want to save the number of associated objects on the has_many side. So if you have Post has many Comments, you can do post#size and no SQL query is executed, Rails simply just looks at the comments_count field of the post.

And - as with so many things in Rails - the beauty is that it just works out of the box, the way you would expect it to work. When you create or delete Comments, Rails keeps the comments_count fields of all associated Posts up to date. Well, most of the time. I have had the situation a couple of times where I ended up with a negative value which obviously doesn't make sense.

Read on

How to get free SSL for your SquareSpace website with CloudFlare

Update January 10 2017: Squarespace now supports SSL for custom domains! There's no need for the shenanigans detailed in this post anymore.

I recently set up SSL for one of my websites which runs on SquareSpace and was amazed by how fast and straightforward it was using CloudFlare - not to mention that it's completely free! Since the only information I could find on this topic is an entry on the SquareSpace help site with some outdated information, I thought I'd document the necessary steps here.

Buckle up!

Read on

How to solve the naked domain problem and forward domain.com to www.domain.com (non-www to www)

A problem every web developer has when starting with a new project is

Do I want to use fancy-new-thing.com or www.fancy-new-thing.com as the URL for my fancy new thing?

This is called the "canonical URL" and it's fairly important that you choose one option and stick to it. If you choose to use the www subdomain, all traffic to the non-www URL should forward to it and keep the path intact, e.g. domain.com/blog/article.html should foward to www.domain.com/blog/article.html

Which option to choose is up to you, there is no right or wrong answer, although many people argue that the www subdomain is not necessary nowadays since it comes from a time where ftp, gopher, telnet were other equally-important subdomains. Many short domains opt to use the non-www version, presumably to make their domain appear even shorter, and it also seems common for .io and .co domains.

Read on

A nifty service layer for your Rails app

If you have been in the Rails world for a while, you have undoubtedly heard about the discussion around whether, as a Rails app grows in size, it needs an additional "service layer" that contains all the business logic. There have been many blog posts about this topic already so I won't repeat the pros and cons here.

Not sure if I need a service layer or just put it all in /lib

I for one started using services as an experiment in a couple of Rails apps I am working on/maintaining in early 2013 and must say, it has made working on these apps enormously more pleasant! Everything from reasoning about the way the apps work to finding bugs or implementing new features has become much more enjoyable!

Read on

code.talks 2014 conference in Hamburg

I am not a big fan of developer conferences I have to admit - too few good talks, too much awkward talk about the latest nerd stuff. The last few conferences haven't been much of an exception, yet I always sign up to new ones thinking I have to "get out there" and can't just work on my own all the time.

Then, when I'm at the conference, what I mostly do (after a few of those awkward nerdy talks) is sit in a quiet corner and work on my projects. Granted, doing that is very nice, since you are surrounded by developers, which is a great environment to be in when coding, but it's not the sole purpose of a going to a conference, now is it?

Read on

Spotcar im Test - was kann der neue Konkurrent zu DriveNow, Car2Go und Multicity?

Spotcar im Test - was kann der neue Konkurrent zu DriveNow, Car2Go und Multicity?

Normally I blog here in English about programming stuff, this post is an exception since it's in German and about a new carsharing provider in Berlin.

Update (12.5.2015): Spotcar ist pleite!

Gestern, am 21. August 2014, ist Spotcar, der neue Carsharing-Anbieter von Opel, offiziell in Berlin gestartet. Da ich ein großer Carsharing-Enthusiast und regelmäßiger Nutzer von fast allen Anbietern (DriveNow, Car2Go, Multicity, Stadtmobil, Flinkster und Citee) bin, habe ich es mir nicht nehmen lassen, sofort eine Testfahrt zu unternehmen.

Das Ergebnis war leider nicht durchweg positiv, aber ich hoffe, es handelt sich bei allen Mängeln, die mir aufgefallen sind, um sogenannte Kinderkrankheiten.

Read on

Extract your common Ruby gems for increased maintainability

After six years of writing Rails apps, for clients as well as side projects (and not-so-side projects), I started seeing a clear pattern in my Gemfiles, namely a bunch of gems I used over and over again.

Well, actually I probably started seeing those patterns already on the third or fourth Rails app, but by now I can be fairly certain that there are some gems that I will want to use in every project I work on.

Read on

Bringing Bootstrap and Ruby together

I've always been a huge fan of Bootstrap, the front-end framework started by two former Twitter employees. For me as a design-impaired programmer, it meant I could build front-ends for my projects all by myself, and they wouldn't look absolutely horrible.

Since I create all of my projects in Ruby (web apps as well as web sites), over the years I have searched for, found, and used (or not found and created myself) my fair share of Ruby wrappers for the different components Bootstrap offers. Often, when I found yet another Bootstrap related Ruby project on Github that did the same as another one, I have wished for a curated collection of those tools, which is exactly what I want to introduce in this post.

Read on

Introducing RubyDocs - Fast and searchable Ruby and Rails docs

Update June 1 2014: RubyDocs is now mentioned as reference documentation on the official Ruby website (look for "Ruby & Rails Searchable API Docs")!

Update May 25 2014: The source code for RubyDocs is now open source under the MIT license and available on Github. If you have any ideas for new features or run into any bugs while using RubyDocs, please open an issue!

RubyDocs was already launched at the end of 2013 but a few days ago I added the most-asked-for missing feature (being able to generate docs for just Ruby or Rails) and noticed that I have never officially introduced the project in a blog post, so here we go.

TL;DR

  • RubyDocs allows to you to generate sdoc docs for any version of Ruby or Rails (or both combined).
  • sdoc is awesome! (it also powers the official Rails API docs)
  • railsapi.com, which used to let you create sdoc docs, has been defunkt for a long time now, which is why I set out to build a replacement.
  • Does anyone know Vladimir Kolesnikov? He owns railsapi.com and I tried to get in touch with him to ask him to forward railsapi.com to rubydocs.org but didn't get an answer.
Read on

How to migrate your websites from UptimeRobot to StatusCake with Ruby

UptimeRobot, which I have been a happy user of since 2010, has been failing me in the last months, reporting hundreds of false downtime alerts for my 34 websites monitored by them. Since beginning of March it has been particularly bad with 20-30 false alerts per day.

I tried to get in touch via Twitter and email a couple of times but received no response so it was time to say goodbye.

Read on