A Love Letter

It’s Valentine’s day.

Love is a rare thing to find. In this line of work we’re always talking about passion, about magic, about how users "love" this thing or the other. But truth be told, it’s not very common. We’ll always shoot for excellence and we’ll always try to achieve that special sparkle—the one that can only come from little decisions made right, the one that makes products come alive. That’s why you hire us, right? That’s what we do.

Love, though?

The stars have to align. We’re not even sure all products or companies are made to be loved. Some are made to be indispensable, some are made to be intensely functional, and some try to help you by staying out of your way. And that’s fine. We all want different things, and everyone finds their own road to happiness. But you know what? When love does strike, you kind of want to shout it from the rooftops. And… we have something to tell you.

Ours, as a company, is not the romantic kind of love. It’s more of a two-hearts-beating-as-one sort of affair, maybe described as the way you look at a friend after a few years and you realize that it’s them, it was them all along, and you never saw it coming.

It struck us right in the face. We used to be Elabs and Varvet. But we’re one and the same now and it’s Valentine’s day, a day for celebrating love and sipping pink champagne, and the path before us looks strewn with rose petals. Join us in walking it.

All this is to say: we’ve merged with Varvet. There used to be two separate companies, awesome in their own way and doing almost the same thing. From now on there’s just one company, called Varvet; twice as strong and at least twice as clever. We’ll still be building superlative digital experiences, there’s just more of us now. Feel free to contact us!


The Year 2015: A Summary

I missed my annual recap last year, mostly because I was tired after a very stressful year, but it’s time to pick up the tradition again. Here are some of the highlights from 2015.

Client projects

We had another year of great client collaborations. During the first half of the year we worked on several interesting projects together with Purpose, including a social media monitoring dashboard for the climate change awareness campaign Here Now, the One Second March campaign, and other work for non-profits like the Bill & Melinda Gates Foundation and Oxfam. We also helped our friends at Oktavilla with some work on

We were also fortunate to work with great companies like Telenor Connexion with their Internet-of-Things (IoT) platform, and Blue Box with their private cloud solution. Both of those projects are continuing next year too.

We continued working with some of our exciting startup clients, like Naturkartan and Pinracer. We also worked with FootballAddicts on our first real Elixir project. We hope to do more Elixir in 2016, so get in touch if you need any Elixir help.


Towards the end of the year we got into online newspaper design with Mitt i, something we're also hoping to do more of in 2016.


In between client projects we finally found time to launch a brand new version of our team scheduling SaaS product ProjectPuzzle. We’ve continued to improve it throughout the year, and we have much more planned. For me personally, it is completely indispensable to running the Elabs consulting business. It let’s me see our utilization at a glance, and keep track of who’s working on what project and when. And with the improvements we’re making, pretty soon it will be a full solution to managing a client services business.



During Lab Days and downtime between client projects Anders and Robin created our first own iOS app: Flip, a beautiful version of the classic board game Reversi. Flip is free on the App Store, give it a try!

Flip Reversi game

Journeyman tour visit

In June we had a visit from Lennart who spent a week with us as part of his journeyman tour. Lennart is always a vitamin injection, and we really enjoyed having him with us. We finished the week with a Lab Day where we did some Elixir and Phoenix mob programming. Great fun!

Lennart wrote a blog post about his journeyman visit with Elabs, you should read it.

Lennart's visit

Nordic Ruby and True North reunion

After running conferences for 5 years straight we decided to take a break during 2015 to catch our breath a bit. We couldn’t stay away from our regular venue Yasuragi completely though, so in early July we held at Nordic Ruby and True North reunion where we invited past attendees to come hang out with us in the serene Japanese spa in the Stockholm archipelago. To our delight, we had quite a few attendees come from as far away as the US to spend the weekend with us!

Reunion at Yasuragi

While it was nice to take a break and focus on our core business, we really miss organizing a conference. There’s a good chance that we’ll do one in 2016. Follow @elabs on Twitter for updates!

Team changes

The biggest changes this year were that we said goodbye to two of our team members.

Johannes—one of our designers—left in July after 5 great years with us to work on a new product idea he had.


Kim, who joined us as a developer 4 years ago, left right before Christmas to move back to Stockholm where his family lives.


We’ll miss them both and wish them the very best of luck!

It wasn’t all goodbyes though! In November we welcomed Max, our second apprentice. Max has been doing great so far, and we’re excited to have him on the team. He introduced himself here on the blog a few weeks ago.

What’s next?

As I’ve hinted at on Twitter, we have some very exciting things in the works for next year! I won’t give it away quite yet, but you should follow us on Twitter if you want to be among the first to know.

I can’t wait for 2016 to begin!

Happy New Year!

/ CJ


A Christmas tree formatter for RSpec!

It's December, a time of glögg and gingerbread cookies with blue cheese… and Christmas trees!

Me and Anders figured we'd bring some Christmas spirit into our test suite, so we created an RSpec formatter very much like the regular progress formatter with green dots… except it's a tree!

Source code is on GitHub, v0.1, and a nasty piece of code, but it's December so it's very much time to ship it! It's named christmas_tree_formatter.

Here's a GIF of it in action:



The Apprentice

I am not the new apprentice of Sir Alan Sugar, as in the TV series The Apprentice. But I am the new apprentice of Elabs, and for that I am very grateful! The first week is soon completed and wow, I already love this place. A spectacular office with awesome people. Everyone here has been treating me so nicely. Except that they all seem to totally crush me in ping-pong. Maybe that is how it’s supposed to be though for a rookie.

Anyhow, my name is Max Perzon and I am a 24 year old web developer student from the school YRGO here in Gothenburg. I am spending the last six months of my education out in the “field” as an apprentice, and that is why I am here at Elabs. What excites me the most about being here is the opportunity I get to be around people way above my skill level for six months. Exposing my ignorance and learning as much as I can by osmosis. Seeing how they attack obstacles and solve problems together. There is no doubt in my mind that I will learn a lot from this experience.

I live together with my girlfriend Åsa in a small apartment here in Gothenburg. She’s from northern Sweden, Östersund. So we like to ski quite a lot together. Other big interests of mine are reading books, running and traveling. I also like dogs, they are awesome.

To round this up, I just want to say thank you to everyone in the team here for welcoming me in such a nice way. It means a lot. I’m looking forward to continuing to get to know each and everyone of them better as time passes. And also, hopefully “steal” some of the secret weapons in their coding arsenal.

Over and out.

— Max Perzon


Retrieving the last N ordered records with ActiveRecord

Here's a database of chat messages.

|  id  | created_at |
|   1  |   13:20    |
|   2  |   13:21    |
|   3  |   13:22    |
|   …  |   ……………    |
|  121 |   14:27    |
|  122 |   14:29    |
|  123 |   14:32    |

So, we're building a chat, and what we'd like to see is the 10 most recent messages, with the oldest of the bunch at the top, and the newest message at the bottom, something like: [<Message 113>, <Message 114>, <Message 115>, …, <Message 123>].

At first, you might think "I'll just sort all messages by created_at in ascending order, and take the last 10". OK, here's what that looks like.

Message.order(created_at: :asc).last(10) # => [<Message 113>, <Message 114>, …, <Message 123>]

Looks good? Yes? No. Have a look at the SQL:

 SELECT "messages".* FROM "messages"  ORDER BY "messages"."created_at" ASC

   |  id  | created_at |
-> |   1  |   13:20    |
-> |   2  |   13:21    |
-> |   3  |   13:22    |
-> |   …  |   ……………    |
-> |  121 |   14:27    |
-> |  122 |   14:29    |
-> |  123 |   14:32    |

What, no mention of 10 in our SQL query?! .last is not so clever. We end up loading all messages in our database to Ruby, maybe a few hundred thousand, and then we throw away all messages except for the last 10, what a waste.

Okay, how about using OFFSET? Let's try.

Message.order(created_at: :asc).offset(Message.count - 10).limit(10) # => [<Message 113>, <Message 114>, …, <Message 123>]        

Looks good? Yes? No. Let's look at the SQL:

 SELECT "messages".* FROM "messages"  ORDER BY "messages"."created_at" ASC OFFSET 112 LIMIT 10

     |  id  | created_at |
SKIP |   1  |   13:20    |
SKIP |   2  |   13:21    |
SKIP |   …  |   ……………    |
  -> |  113 |   14:13    |
  -> |   …  |   ……………    |
  -> |  123 |   14:32    |

A few important notes about this.

  1. OFFSET in SQL must be a positive number so we can't simply use -10 as our offset.
  2. ActiveRecord will try to coerce our offset to an integer using #to_i, so we can't pass a subquery as our offset, which leaves us with precomputed positive numbers only.
  3. What is our offset? It must be calculated ahead of time using Message.count, this is slow, and prone to race conditions if we get more messages in between our count and select.

Even if we disregard all the above points, have a look at the documentation for LIMIT/OFFSET in PostgreSQL: "The rows skipped by an OFFSET clause still have to be computed inside the server; therefore a large OFFSET might be inefficient."

Oh, OK, so offset is off.

One more try. Let's sort this out with a new mindset. We change the order of our SQL, and reverse the thing in Ruby. That must work!

Message.order(created_at: :asc).reverse_order.limit(10).reverse # => [<Message 113>, <Message 114>, …, <Message 123>]

Looks good? Yes? Let's look at the SQL.

 SELECT "messages".* FROM "messages"  ORDER BY "messages"."created_at" DESC LIMIT 10

   |  id  | created_at |
-> |  123 |   14:32    |
-> |   …  |   ……………    |
-> |  113 |   14:13    |

Well, this is actually quite OK. We only retrieve 10 records from the database, which is what we want. Sure, the order of the messages is wrong which is kind of sad, but we can fix that later. Let's make a scope of this and call it a good day!

class << Message
  def in_order
    order(created_at: :asc)

  def recent(n)

There's a few downsides with this, can you spot it?

  1. We have to reverse it in Ruby.
  2. The return value is an Array, and not an ActiveRecord::Relation, since we force it with reverse.
  3. We can't merge an Array with other scopes.
  4. We can't chain additional SQL conditions to the end of our array to further filter the 10 results, e.g. Message.recent(5).where(…).

We can still do better!

What if I told you there is a way to reverse the result in SQL, and that there's also an OK way to do so with ActiveRecord? I'm sure you'd believe me after all of this, anything else would be cruel.

class Message
  class << self
    def in_order
      order(created_at: :asc)

    def recent(n)

    def endmost(n)
      all.only(:order).from(all.reverse_order.limit(n), table_name)

Looks good? Yes! Let's use this and have a look at the SQL, Message.recent(10):

SELECT "messages".* FROM (
  SELECT  "messages".* FROM "messages"  ORDER BY "messages"."created_at" DESC LIMIT 10
) messages  ORDER BY "messages"."created_at" ASC

   |  id  | created_at |
-> |  113 |   14:13    |
-> |   …  |   ……………    |
-> |  123 |   14:32    |

This is exactly what we want, and it has none of the downsides of reversing the results in Ruby. The final .recent method is our final implementation, and it works as expected.

The keen eye will notice that I extracted part of the logic into a general-purpose .endmost method. .endmost is what you want when you call .last: the last N records in the result set without having to retrieve all records from the database, and it works with any ordering. You can impose filtering before, Message.where(…).in_order.endmost(10), and afterwards to filter your final results as well Message.in_order.endmost(10).where(…).

Thanks for reading! I hope you found it as useful as I did!