How Rails and MySQL are handling time zones

When working with times and dates in Rails applications, you need to deal with the following problem:

  • In Rails, Time objects have a time zone. You can get the zone name by doing
  • This zone is considered when doing time calculations, e.g. 10 AM CEST minus 8 AM UTC is zero.
  • A datetime in MySQL does not have a zone. It just stores the literal string "2010-05-01 12:00:00".
  • That means that Rails must make assumptions about timestamps loaded from and written to MySQL.

Rails has two completely different modes …

PostgreSQL: How to UPDATE multiple attributes with multiple joins

This is an extension to PostgreSQL vs MySQL: How to UPDATE using a JOIN.

UPDATE employees
SET department_name =,
  department_area =
FROM departments, areas
WHERE employees.department_id =
AND = areas.department_id

Capistrano: How to find out which version of your application is currently live

When deploying, Capistrano will put a REVISION file into your application's release directory. It contains the hash of the commit which was deployed.

You can just SSH to a server and do this:

cat /opt/www/my-project/REVISION

If you are deploying to multiple machines, you'd want to run that command on each server.

This Capistrano task will run it on all your project's servers that have the :app role:

desc 'Show deployed revision'
deploy.task :revision, :roles => :app do
run "ca…


How to write complex migrations in Rails

Rails gives you migrations to change your database schema with simple commands like add_column or update.
Unfortunately these commands are simply not expressive enough to handle complex cases.

This card outlines three different techniques you can use to describe nontrivial migrations in Rails / ActiveRecord.

Note that the techniques below should serve you well for tables with many thousand rows. Once your database tables grows to millions of rows, migration performance becomes an iss…

RSpec: How to check that an ActiveRecord relation contains exactly these elements

To check which elements an ActiveRecord relation contains use the contain_exactly matcher.

describe User do
  let!(:admin) { create(:user, role: 'admin') }
  let!(:customer) { create(:user, role: 'customer') }
  subject(:users) { User.where(role: 'admin') }
  # Recommended (The error output is most helpful)
  it { expect(users).to contain_exactly(admin) }
  # Other options
  it { expect(users).to eq([admin]) }
  it { expect(users.pluck(:id).to eq([]) }

In case you have an ActiveRecord::AssociationRelation

jQuery: How to attach an event handler only once

With "attaching an event handler once" you possibly mean one of these two things:

Register a function for an event, and discard it once that event has happened

Use one instead of on.

$(element).one('eventName', function() { ... });

It has the same API has on.

When code is run multiple times that registers a function for an event, do that only once

With jQuery, you can de-register callbacks. You can use that to achieve registering a function only once.

function myAction() { ... }; // defined somewhere globally o...

Capybara: A step for finding images with filename and extension

This cucumber step is useful for testing an image (looking at the src of the image).

Then(/^I should see the image "([^"]*)"$/) do |filename_with_extension|
  expect(page).to have_css("img[src*='#{filename_with_extension}']")
Then I should see the image "placeholder.png"

Outline: Read more about how to test a uploaded file here, e.g. file downloads.

External content

About-Payments: Platform to compare payment providers

About-Payments is here to help you to accept payments online and
find the best payment service provider for your e-commerce business.

Sprites with Compass

Using CSS sprites for background images is a technique for optimizing page load time by combining smaller images into a larger image sprite.

There are ongoing arguments on how useful this still is, as modern browsers become more comfortable to load images in parallel. However, many major websites still use them, for example amazon, [facebook](…

External content

Font Awesome 5 migration guide

Font Awesome version 5 changed some icon names, and introduces new prefixes fab, far, and fas.

There is a JavaScript shim that you can use as a "quick fix". It allows you to keep v4 icon names on v5.

The linked page includes details on using that, and a migration guide including a list of icon renames.


Deterministic ordering of records by created_at timestamp

Creating records in specs can be so fast that two records created instantly after one another might have the same created_at timestamp (especially since those timestamps don't have an indefinitely high resolution). When ordering lists by timestamps, you should therefore always include a second order condition using the primary key of the table.

Photo.order('created_at DESC, id DESC')
# or
Photo.order(created_at: :desc, id: :desc)
# => SELECT * FROM photos ORDER BY created_at DESC, id DESC

Of course, you can also do this ordering …

Ruby: control flow with throw and catch

This article will show you how to use throw and catch. It's a nice tool to break out of multiple loops when a result is obtained.

Also see our card Ruby: A small summary of what return, break and next means for blocks.

How to debug Rails autoloading

ActiveSupport::Dependencies takes care of auto-loading any classes in development. This is usually useful, but when you run into issues with the Rails autoloader, you should take a look at what it's doing.

For me this was useful in an "exciting" case of auto-loading classes inside a thread which caused the application to stop responding.

Rails 4.x

ActiveSupport::Dependencies includes logging support. It is easy to use:

ActiveSupport::Dependencies.logger = Rails.logger

Rails 5+

[Logging support was removed](https://github…


Linux: Open a file with the default application

If you are on a Linux shell and want to open a file with whatever default application is configured for that type, this often works:

xdg-open Quote.odt
xdg-open invoice.pdf
xdg-open index.html

Make an alias so you have a simpler API (like Mac OS): alias open=xdg-open.

How to query PostgreSQL's json fields from Rails

PostgreSQL offers a really handy field type: json. You can store any JSON there, in any structure.

While its flexibility is great, there is no syntactic sugar in Rails yet. Thus, you need to manually query the database.


# Given a Task model with 'details' json column
Task.where("details-»'key' = ?", "value") # matches where 'details' contains "key":"value"
Task.where("details-»'{a,b}' = ?", "value") # matches where 'details' contains "a":{"b":"value"}
Task.where("details->'a'-»'b' = ?", "value") # same as above, but via …


Carrierwave: Deleting files

TL;DR Use the #remove_<mounted_attribute>! method to delete attachments. Also, save the record after removing the file!

As you know, Carrierwave file attachments work by mounting an Uploader class to an attribute of the model. Though the database field holds the file name as string, calling the attribute will always return the uploader, no matter if a file is attached or not. (Side note: use #present? on the uploader to check if the file exists.)

class User < ApplicationRecord
  mount :avatar, AvatarUploader

External contentRepeats

Everything you ever wanted to know about constant lookup in Ruby

If you ever wondered why a constant wasn't defined or wasn't available where you expected it to be, this article will help.

Also see Ruby constant lookup: The good, the bad and the ugly.


Rails and Postgres: How to test if your index is used as expected

This is a small example on how you can check if your Postgres index can be used by a specific query in you Rails application. For more complex execution plans it might still be a good idea to use the same path of proof.

1. Identify the query your application produces

puts User.order(:last_name, :created_at).to_sql
# => SELECT "users".* FROM "users" ORDER BY "users"."last_name" ASC, "users"."created_at" ASC

2. Add an index in your migration and migrate

add_index :users, [:last_name, :created_at]

3. Test the…

3474 cards