Traverse an ActiveRecord relation along an association

Posted About 11 years ago by Henning Koch.

The Edge Rider gem gives your relations a method #traverse_association which returns a new relation by "pivoting" around a...

The many gotchas of Ruby class variables

Posted About 11 years ago by Henning Koch.

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

Regex: Be careful when trying to match the start and/or end of a text

Posted About 11 years ago by Thomas Eisenbarth.

Ruby has two different ways to match the start and the end of a text: ^ (Start of line) and $ (End...

Understanding race conditions with duplicate unique keys in Rails

Posted Over 11 years ago by Henning Koch.

validates_uniqueness_of is not sufficient to ensure the uniqueness of a value. The reason for this is that in...

How to update a single gem conservatively

Posted Over 11 years ago by Henning Koch.

Calling bundle update GEMNAME will update a lot more gems than you think. E.g. when you do this...

Running "bundle update" without arguments might break your application

Posted Over 11 years ago by Henning Koch.

Calling bundle update (without arguments) updates all your gems at once. Given that many gems don't care about stable...

Custom error pages in Rails

Posted Over 11 years ago by Tobias Kraze.

Basic error pages To add a few basic styles to the default error pages in Rails, just edit the default...

Git: Improve your commits by reviewing changes one-by-one

Posted Over 11 years ago by Tobias Kraze.

Git commits should be very deliberate, and only contain changes that you really want to be in there. In order...

Don't name columns like counter_cache columns

Posted Over 11 years ago by Henning Koch.

ActiveRecord has a feature called counter caching where the containing record in a has_many relationship caches the number of...

Rails: When to use :inverse_of in has_many, has_one or belongs_to associations

Posted Over 11 years ago by Henning Koch.

When you have two models in a has_many, has_one or belongs_to association, the :inverse_of option in...

`simple_format` does not escape HTML tags

Posted Over 11 years ago.

simple_format ignores Rails' XSS protection. Even when called with an unsafe string, HTML characters will not be escaped or...

How to make your application assets cachable in Rails

Posted Over 11 years ago by Henning Koch.

Note: Modern Rails has two build pipelines, the asset pipeline (or "Sprockets") and Webpacker. The principles below apply for both...

Ruby tempfiles

Posted Over 11 years ago by Andreas Robecke.

Tempfiles get deleted automatically With the the ruby Tempfile class you can create temporary files. Those files only stick around...

Git: How to rebase your feature branch from one branch to another

Posted Almost 12 years ago by Arne Hartherz.

In a nutshell: Use git rebase --onto target-branch source-commit target-branch means "branch you want to be based...

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

Posted Almost 12 years ago by Arne Hartherz.

When deploying, Capistrano puts a REVISION file into your application's release directory. It contains the hash of the commit...

Ruby: Replacing Unicode characters with a 7-bit transliteration

Posted Almost 12 years ago by Henning Koch.

Using ActiveSupport ActiveSupport comes with a #transliterate method which replaces characters with their low-ASCII equivalent (to strip accents etc...

Beware of "nil" values in params

Posted Almost 12 years ago by Tobias Kraze.

Recent rails security updates have shown that people make incorrect assumptions about the possible contents of the params hash.

Organize large I18n dictionary files in Ruby on Rails

Posted Almost 12 years ago by Henning Koch.

If you're suffering from a huge de.yml or similiar file, cry no more. Rails lets you freely organize your...

Guide to localizing a Rails application

Posted Almost 12 years ago by Henning Koch.

Localizing a non-trivial application can be a huge undertaking. This card will give you an overview over the many...

How to use pessimistic row locks with ActiveRecord

Posted Almost 12 years ago by Thomas Eisenbarth.

When requests arrive at the application servers simultaneously, weird things can happen. Sometimes, this can also happen if a user...

Authorize allowed values with assignable_values

Posted About 12 years ago by Henning Koch.

All our projects have enum-like requirements like this: An attribute value must be included in a given set of...

Google Analytics: Change the tracked URL path

Posted About 12 years ago by Henning Koch.

By default, Google Analytics tracks the current URL for every request. Sometimes you will want to track another URL instead...

Why has_many :through associations can return the same record multiple times

Posted About 12 years ago by Henning Koch.

An association defined with has_many :through will return the same record multiple times if multiple join models for the...

Git: How to configure git to push only your current branch

Posted About 12 years ago by Arne Hartherz.

You can change which branches will be pushed when saying git push. Our recommendation is to set it to current...