Don't sum up columns with + in a SQL query if NULL-values can be present.

Don't sum up columns with + in a sql-query if NULL-Values can be present.

MySQL and PostgreSQL cannot sum up NULL values with the + value. The sum value will be NULL.


mysql> select 1 + 2 + 3;
| 1 + 2 + 3 |
|         6 |
1 row in set (0,00 sec)

mysql> select 1 + NULL + 3;
| 1 + NULL + 3 |
|         NULL |
1 row in set (0,00 sec)



test_database=# select 1 + 2 + 3;


    6 (1 row)


External contentRepeats

What Ruby’s ||= (Double Pipe / Or Equals) Really Does

||= and &&=

a ||= b
a || a = b

a &&= b
a && a = b


a += b
a = a + b

a -= b
a = a - b

# Other operators, e.g. Array#| (union)
ary1 = ['a', 'b', 'c']
ary2 = ['c', 'd', 'a']

ary1 |= ary2
ary1 = ary1 | ary2
# => ['a', 'b', 'c', 'd']

Understanding SQL compatibility modes in MySQL and MariaDB

MySQL and MariaDB have an SQL mode setting which changes how MySQL behaves.

The SQL mode value is comprised of multiple flags like "STRICT_TRANS_TABLES, NO_ZERO_IN_DATE". Each flag activates or disables a particular behavior.

The default SQL mode varies widly between versions of MySQL and MySQL. In general, more recent versions of MySQL and MariaDB have stricter settings than older versions, and MySQL has stricter settings than the more liberal MariaDB.

If your app explodes af…

Store MySQL passwords for development

On your local system that only hosts non-critical development data and only you have access to, you can store MySQL's root password in your home directory so you can use mysql commands without prompt for passwords, i.e. when doing batch processing.

Of course, this has security implications. The password must be stored in plain text, so this method is out of the question if there's any confidential data in your databases. Assuming diligent screen-locking, an encrypted hard disk and correct permissions set on your credential file, it shoul…


Don't assign time values to date attributes

Do not pass times to date attributes. Always convert times to dates when your application uses time zones.


A time-zoned Time attribute on a Rails record is converted to UTC using to_s(:db) to be stored, and converted back into the correct time zone when the record is loaded from the database. So when you are not on UTC, time objects will be converted as follows.

>> Time.current
=> Fri, 15 Mar 2013 11:56:03 CET +01:00
>> Time.current.to_s(:db)
=> "2013-03-15 10:56:03" # This is now UTC


That will…


FileIO: Writing strings as Carrierwave uploads

When you have string contents (e.g. a generated binary stream, or data from a remote source) that you want to store as a file using Carrierwave, here is a simple solution.

While you could write your string to a file and pass that file to Carrierwave, why even bother? You already have your string (or stream).
However, a plain StringIO object will not work for Carrierwave's ActiveRecord integration:

>> Attachment.create!(file:
TypeError: no implicit conversion of nil into String

This is because Carrierwave ex…

Auto-destruct in 58 days

Unpoly 0.54.0 released

There were a number of Unpoly releases addressing issues that we encountered in our current development projects:


Passive updates

  • [up-hungry] elements will now also be updated when the server responds with an error code. This helps when [up-hungry] is used to display error messages.


  • When a form is submitted you can now consistently refer to that form element as ampersand (&) in CSS selectors ([like in Sass](…

Git: Advisory for cherry-picks to production branches

We often have a separate production branch that lags a bit behind the more cutting edge master branch. Sometimes you want to move some, but not all commits from master to production. This can be done with a git cherry-pick.

However, this may lead to considerable pain later, since git does not understand the commits are actually "the same". Hazards are unnecessary and hard to resolve conflicts as well as incorrect auto-merges.

In order to avoid this, always merge the production branch back to the master after the cherry-pick. …

Auto-destruct in 55 days

Updated: Nested Spreewald patiently blocks are now patient

Spreewald 1.11.2 simplifies the patiently logic to

"Retry patiently blocks until the wait time is over and the block has been retried at least once".

This seems to work for all our use cases with nested patiently blocks, but does not have the downside of taking extremely long if the test is actually red.


Common mistakes when storing file uploads with Rails

1. Saving files to a directory that is not shared between deploys or servers

If you save your uploads to a made up directory like "RAILS_ROOT/uploads", this directory goes away after every deploy (since every release gets a new). Also this directory is not shared between multiple application servers, so your uploads are randomly saved to one local filesystem or another. Fixing this afterwards is a lot of fun.

Only two folders are, by default, shared between our application servers and deployments: "RAILS_ROOT/storage" and `"RAILS…

Rails: Adding view paths on the fly

Rails offers a way to prepend (or append) view paths just for the current request.


A use case of this is a different set of view templates that should be used under certain circumstances:

class SomeController < ApplicationController

  before_action :prepare_views
  # ...
  def prepare_views
    if <condition>
      prepend_view_path Rails.root.join('app', 'views', 'special')

If <condition> is true, Rails will first look into app/views/special to find a view template. If the …

Upgrading a Rails app to Cucumber 3

Upgrade gems

You need to update a lof gems. Make sure you don't have any version constraints in your Gemfile or your bundle update won't do anything!

Upgrade cucumber_priority:

bundle update cucumber_priority

Upgrade spreewald:

bundle update spreewald

Upgrade cucumber_factory:

bundle update cucumber_factory

Upgrade parallel_tests:

bundle update parallel_tests

Even on the latest version, parallel_tests will print some deprecation warnings due to using an older formatter A…

RubyMine: You can disable inspections you don't care about

When you find yourself constantly ignoring a RubyMine warning, you can simple disable that warning and de-clutter your editor. E.g. in my Cucumber scenarios RubyMine underlines 90% of all lines because it does not know about spreewald, making the file really hard to read.

You can disable any unwanted inspection by opening File / Settings / Editor / Inspections and searching for the warning text.

What you disable or keep is up to your personal preference. I personally disable at least the following…

Running Rails 2 apps with modern MariaDB SQL server

You might have some trouble running a Rails LTS 2 app with MySQL 5.7.

If you don't want to hack Mysql 5.6 into your modern Ubuntu or use the MySQL sandbox, you might want to try MariaDB 10.x.

MariaDB 10.x should work with both old and new Rails applications.

Switch to MariaDB

Remove MySQL:

sudo apt r…

Auto-destruct in 50 days

Updated: How to make Webpacker compile once for parallel tests, and only if necessary

The script now detects changes in your npm dependencies.

You should no longer need to remove public/packs-test when you switch branches and the other branch has added or upgraded an npm package.


Detect the current Rails environment from JavaScript

Detecting if a Javascript is running under Selenium WebDriver is super-painful. It's much easier to detect the current Rails environment instead.

You might be better of checking against the name of the current Rails environment. To do this, store the environment name in a data-environment of your <body>. E.g., in your application layout:

%body{'data-environment' => Rails.env}

Now you can say in a piece of Jav…

Making httpclient use the operating system's SSL cert store

The httpclient gem comes with a custom SSL cert store.

While an customizable, application-level cert store is great when you need to deal with broken or self-signed certificates, you usually want to use the cert store from the underlying Linux. The Linux cert store is updated periodically while httpclient's cert store goes out of date and will eventually not be able to verify certs.

To use the cert store from the underlying operating system:

client =


RSpec: be_true does not actually check if a value is true

Don't use be_true to check if a value is true. It actually checks if it anything other than nil or false. That's why it has been renamed to be_truthy in recent RSpec versions.

The same thing holds for be_false, which actually checks if a value is not "truthy".

If you want to check for true or false in RSpec 2, write this instead:

value.should == true
value.should == false

If you want to check for true or false in RSpec 3+, write this instead:


3447 cards