Don't mix Array#join and String#html_safe

Posted Over 13 years ago by Henning Koch.

You cannot use Array#join on an array of strings where some strings are html_safe and others are not...

Preloaded associations are filtered by conditions on the same table

Posted Over 13 years ago by Arne Hartherz.

When you eagerly load an association list using the .include option, and at the same time have a .where on...

Copying validation errors from one attribute to another

Posted Over 13 years ago by Henning Koch.

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

Collect all values for a given column in an ActiveRecord scope

Posted Over 13 years ago by Arne Hartherz.

In modern Rails versions you can also use ActiveRecord's pluck method. >> User.active.pluck(:id) => [1, 5, 23, 42]

Know your Haml comments

Posted Over 13 years ago by Arne Hartherz.

There are two distinct ways of commenting Haml markup: HTML and Ruby. HTML comments This will create an HTML comment...

Load order of the environment

Posted Over 13 years ago by Henning Koch.

Rails 3, 4, 5, 6 config/application.rb config/environment.rb before the initialize! call (we don't usually edit this file)

Preload associations in loaded records

Posted Over 13 years ago by Arne Hartherz.

Sometimes you want to fetch associations for an ActiveRecord that you already loaded, e.g. when it has deeply nested associations...

Efficiently add an event listener to many elements

Posted Over 13 years ago by Henning Koch.

When you need to add a event listener to hundreds of elements, this might slow down the browser. An alternative...