3938 cards
Linked contentRepeats

Databases don't order rows unless you tell them so

There is no such thing as a "default order" of rows in database tables.

For instance, when you paginate a result set: When using LIMIT, it is important to use an ORDER BY clause that constrains the result rows into a unique order. Otherwise you will get an unpredictable subset of the query's rows. You might be asking for the tenth through twentieth rows, but tenth through twentieth in what ordering? The ordering is unknown, unless you specified ORDER BY.

In Rails, if you use Record.first or Record.last, it will default to orderin...


ActiveRecord: validate_uniqueness_of is case sensitive by default

By default, validates_uniqueness_of does not consider "username" and "USERNAME" to be a collision. If you use MySQL this is probably not what you want, since string comparisons are case-insensitive in MySQL.

(If you use PostgreSQL, read this instead.)

Say you have a user model

class User < ActiveRecord::Base
  validates_uniqueness_of :name

with a unique index in the database.

If you try to create the users "user" and "USER", this will not trigger a validation error, but may fail with a SQL error due to d...


Large CSS box shadows can bring browsers to a crawl

Browser rendering engines are very slow at rendering large box shadows. I had a situation where a complex layout with fixed elements and large shadows slowed Firefox down to 2 frames/second for scrolling and DOM manipulation.

Some advice:

  • Be aware that by introducing fixed elements (e.g. sticky navigation bars) and large animations, you might force the browser to redraw large portions of the site when scrolling. When your fixed elements have shadows, this increases the screen area that needs to be redrawn, which might again require other...
Linked contentAuto-destruct in 58 days

Unpoly 2.4.0 released

How to put binary strings into your Ruby source

Ruby source code is interpreted as UTF-8 by default. If you want to declare binary Strings directly in your code, you can not write them verbatim as Strings, even when using escape sequences.

Note that you rarely want or need to do this. It can be useful to specify a binary key in your code or some tests at some point, and here is how to do it.

Also note that your default encoding may be different, e.g. due to a different LANG env variable in your machine; the contents of this card still apply.


Consider a (binary) String wi...

Rails: Removing the cucumber-rails warning when setting cache_classes to false without Spring enabled

We are using Spring in our tests for sequential test execution but not for parallel test execution. And Rails requires you to set the config.cache_classes = false if you are using Spring in tests.

With our setup, this would raise the following error in cucumber-rails Archive for parallel test executions due to some legacy database cleaner issue.

WARNING: You have set Rails' config.cache_classes to false
    (Spring needs cache_classes set to false). This is known to cause probl...

Working on the Linux command line: How to use bookmarks for directories

Bookmarks for directories will be most helpful if you are forced to work in deeply nested projects. Then it's really helpful!

This makes use of the CDPATH variable. Similar to the PATH variable, which holds the list of directories which are searched for executables, CDPATH contains the list of directories that are available for cd. Besides the current directory (.), you can add others to that.

The trick is to add a directory for bookmarks to CDPATH.

First, create the directory with: mkdir ~/.bookmarks.

Then add the followin...

Working on the Linux command line: How to efficiently navigate up

With cd .. you can navigate one directory up from the one you are at now. If you use that a lot, consider some handy aliases.

Add the following lines to your ~/.bashrc:

alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
alias ......="cd ../../../../.."

you can add even more aliases, but I usually loose track after too many levels and just jump to the directly directly, e.g. using its absolute path or its bookmark (see [this card](https://makandracards.com/makandra/504947-working-on-the-li...

Working on the Linux command line: Use the `tree` command instead of repeated `cd` and `ls`

The tree command will show you the contents of a directory and all it's sub directories as a tree:

├── a
│   ├── file_1.txt
│   └── file_2.txt
└── b
    ├── c
    │   └── even_more.txt
    └── more.txt

3 directories, 4 files

If you have deeply nested directories, the output will be quite long though. To avoid that, you can limit the depth, e.g. tree -L 2 will only go 2 directories deep.

If you use that regularly, consider adding aliases for that to your ~/.bashrc:

alias tree2='tree -L 2'
alias tree3='tree -L 3'...

Working on the Linux command line: How to bash `cd` with autocorrect

There is an option you can set so that when using the cd command, small typos are automatically corrected. Add the following to your ~/.bashrc:

# cd: autocorrect small typos and use best guess
shopt -s cdspell 


>cd Porjects

Also, I recommend adding aliases for your most common typos of commands you regularly use to your ~/bashrc. Which ones that are is highly personal, for me it's e.g. tig:

alias tog='tig'
alias tug='tig'

The many gotchas of Ruby class variables

TLDR: Ruby class variables (@@foo) are dangerous in many ways. You should avoid them at all cost. See bottom of this card for alternatives.

Class variables are shared between a class hierarchy

When you declare a class variable, it is shared between this and all descending (inheriting) classes. This is rarely what you want.

Class variables are bound at compile-time

Like unqualified constants, class variables are bound to your current scope *whe...

Linked contentRepeats

Bash: Build and execute command lines on the fly with "xargs"

xargs is a powerful bash tool that can take input from $STDIN and pass it to a given command. I.e. you can do the following:

$> cat tmp/parallel_cucumber_failures.log

$> cat tmp/parallel_cucumber_failures.log | xargs geordi cucumber
# Running features
> Only: features/authentication.feature:33 features/backend/pages.feature:5 features/backend/pages.feature:60

Beside the linked article you might also be interested in reading ...


Using regular expressions in JavaScript

Regular expressions in Javascript are represented by a RegExp object. There also is a regex literal as in many other languages: /regex/. However, they are used slightly differently.

Regex literal

  • Usage: /foo+/
  • Shorthand for creating a regular expression object

RegExp() object

  • Usage: RegExp("foo+") or new RegExp("foo+")
  • No surrounding slashes required (they're the literal markers)
  • Since the argument is a string, backslashes need to be escaped as well: RegExp("\\d+")


  • Regex objects [never eq...
Linked content

Encrypting messages with age (alternative to PGP)

age is a simple, modern and secure file encryption tool, format, and Go library.
It features small explicit keys, no config options, and UNIX-style composability.

Generally we are happy with GPG for encrypting emails. In case you are not happy with the CLI of GnuPG, this might be a tool you can use under the hood for encryption.

Linked content

SSL for your local development environment

The linked tool mkcert Archive helps you to setup locally-trusted development certificates. We also have cards that describe how to use SSL in development with Passenger and Thin.

Unpoly 2: Don't try to download files through AJAX requests

Rails has the handy controller method send_file Archive which lets us download files easily. We can decide whether the file should be downloaded (disposition: 'attachment') or shown in the browser (disposition: 'inline'). The default is disposition: 'attachment'.

Downloading files will not work when you are calling the controller action from an AJAX request. The browser will try to render the file and insert it in the DOM, which is never what you want.

Unpoly 2

Unpoly (sin...

This website uses short-lived cookies to improve usability.
Accept or learn more