Obtain row locks on ActiveRecord objects

When requests arrive at the application servers simultaneously, weird things can happen. Sometimes, this can also happen if a user double-clicks on a button, for example.

This often leads to problems, as two object instances are modified in parallel maybe by different code and one of the requests writes the results to the database.

In case you want to make sure that only one of the requests "wins", i.e. one of the requests is fully executed and completed while the other one at least has to wait for the first request to be completed, you ha…

How to fix: "rake db:rollback" does not work

When you run rake db:rollback and nothing happens, you are probably missing the latest migration file (or have not migrated yet).

$ rake db:rollback

If that happens to you, check your migration status.

$ rake db:migrate:status
   up     20160503143434  Create users
   up     20160506134137  Create pages
   up     20160517112656  Migrate pages to page versions
   up     20160518112023  ********** NO FILE **********

When you tell Rails to roll back, it tries to roll back the latest change that was mi…


How to explain SQL statements via ActiveRecord

ActiveRecord offers an explain method similar to using EXPLAIN SQL statements on the database.

However, this approach will explain all queries for the given scope which may include joins or includes.

Output will resemble your database's EXPLAIN style. For example, it looks like this on MySQL:

User.where(id: 1).includes(:articles).explain

EXPLAIN for: SELECT users.* FROM users  WHERE = 1
| id | select_type | table | type  | possible_keys |

External contentRepeats

Rails 3+: strip_heredoc (String) - APIdock

Note: If you're on Ruby 2.3+ there's a <<~ operator to automatically unindent HEREDOCs.

Copied from the documentation:

strip_heredoc() public

Strips indentation in heredocs.

For example in

if options[:usage]
  puts <<-USAGE.strip_heredoc
    This command does such and such.

    Supported options are:
      -h         This message

the user…

External content

Introducing Helix: Rust + Ruby, Without The Glue

Helix allows you to implement performance-critical code of your Ruby app in Rust, without requiring glue code to bridge between both languages.

See attached article for a longer write-up about the why and how.


VCR: Alternative way of mocking remote APIs

If you need to test interaction with a remote API, check out the VCR gem as an alternative to Webmock or stubbing hell.

The idea behind VCR is that is performs real HTTP requests and logs the interaction in a .yml file. When you run the test again, requests and responses are stubbed from the log and the test can run offline.

It's a great way to mock network requests to an external service without going through the pain of logg…

How to: Client-side language detection

When you have a localized website, you may want to redirect users to their preferred language when they visit the root path.
Here is how to do it without a server-side component (like a Rails application).

  • Use JavaScript's navigator.language (real browsers and IE11+) and navigator.userLanguage (old IEs).
  • Use a <meta> refresh as fallback
  • Provide buttons for paranoid users that disabled JavaScript and meta refreshs.


The following JavaScript will try to auto-detect a user's preferred language.

It understands string…

postgres_ext: additional Rails bindings for PostgreSQL

Adds missing native PostgreSQL data types to ActiveRecord and convenient querying extensions for ActiveRecord and Arel for Rails 4.x

Common table expressions

  • Relation#with
  • Model.from_cte


Face.where.contains tags: %w[happy smiling] # Matching faces have both 'happy' and 'smiling' tags
Face.where.overlap tags: %w[happy smiling] # Matching faces have at least one of these tags
Face.where.any tags: 'happy' # Matching faces include the 'happy' tag
Face.where.all tags: 'dunno' # Not documented, try for yourself

How to change the hostname in Cucumber features

Capybara uses as the default hostname when making requests.
If your application does something specific on certain hostnames and you want to test this in a feature, you need to tell Capybara to assume a different host.

Given /^our host is "([^\"]+)"$/ do |host|
  Capybara.stub app_host: "http://#{host}"

You can now say:

When I go to the start page
Then I should not see "Home of Foo"

When our host is ""
  And I go to the start page
Then I should see "Home of Foo"

The ho…

FreeBSD pkg can't find any packages

If you're trying to searching or installing packages via pkg the your repository data might be broken. If pkg update shows that your repositories are up to date try:

pkg update -f

Afterwards you should be able to search and install packages again.


How to Work With Time Zones in Rails

When dealing with time zones in Rails, there is one key fact to keep in mind: Rails does support time zones, but Ruby does not. Thus, using Ruby's time API will neglect time zones and give you wrong results. Don't use Ruby's methods, only use Rails' methods.

The hard thing is to know which method originates from Ruby and which from Rails. To simplify this, adhere to the following suggestion:

» Use for everything time-related: «

``` # instead of, # instead of Date.tod…


Copy validation errors from one attribute to another

When using virtual attributes, the attached trait can be useful to automatically copy errors from one attribute to another.

Here is a typical use case where Paperclip creates a virtual attribute :attachment, but there are validations on both :attachment and :attachment_file_name. If the form has a file picker on :attachment, you would like to highlight it with errors from any attribute:

class Note < ActiveRecord::Base
  has_attached_file :attachment
External content

thoughtbot/fake_stripe: A Stripe fake so that you can avoid hitting Stripe servers in tests.

fake_stripe spins up a local server that acts like Stripe’s and also serves a fake version of Stripe.js, Stripe’s JavaScript library that allows you to collect your customers’ payment information without ever having it touch your servers. It spins up when you run your feature specs, so that you can test your purchase flow without hitting Stripe’s servers or making any external HTTP requests.

We've also had tests actually hitting the testing sandbox of Stripe, which worked OK most of the time (can be flakey).

Prevent RubyMine from reformatting pasted code

When you paste copied code with CTRL+V, RubyMine will change the indentation of the pasted code. You can prevent this by pasting with CTRL+ALT+Shift+V instead ("Paste Simple").

To change this behavior entirely, you can open your settings and navigate to Editor / General / Smart Keys. Here you can select one of three options for Reformat on paste:

  • None
  • Indent Each Line (default)
  • Indent Block
  • Reformat Block

You might want to try "Indent Block".


MySQL: CONCAT with NULL fields


CONCAT('foo', 'bar', NULL) = NULL

the NULL always wins.

If you would rather treat NULL as an empty string, use CONCAT_WS (concatenation with separator) instead:

CONCAT_WS('', 'foo', 'bar', NULL) = 'foobar'

Linux: Manipulate PDF documents

Here are some tools that are useful to manipulate PDF documents under Linux/Ubuntu:

Add text, add images, add geometrical shapes. Horrible GUI, but works some of the time.
Graphical GUI to reorder pages, remove pages, rotate pages, add pages.
Command line tool to rotate, concatenate, repair PDF documents.
LibreOffice Draw
LibreOffice Draw can import a PDF document as a million text boxes. This w…

RSpec: Expecting non-primitive objects as method invocation arguments

Expecting a primitive value as an argument to a method invocation is easy:

expect(object).to_receive(:foo).with('arg1', 'arg2')

This expectation would be met with this call:'arg1', 'arg2')

But what if the argument is expected to be an object with a given set of methods? E.g. this class with #first_name and #last_name methods:

class Person

  def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
  attr_reader :first_name, :last_name

To just t…

3162 cards