Ruby: Making your regular expressions more readable with /x and alternative delimiters

Posted About 12 years ago by Henning Koch.

The following two hints are taken from Github's Ruby style guide: If your regular expression mentions a lot of...

Do not rescue inline in Ruby

Posted About 12 years ago by Arne Hartherz.

When you are calling a method that may raise an exception that you don't care about, you might think...

Gatekeeping: Guide for developer

Posted Almost 12 years ago by Tobias Kraze.

Note: This process is tailored to our specific needs and tools at makandra. While it will certainly not apply to...

ActiveRecord: When aggregating nested children, always exclude children marked for destruction

Posted About 12 years ago.

When your model is using a callback like before_save or before_validation to calculate an aggregated value from its...

Don't compare datetimes with date ranges in MySQL and PostgreSQL

Posted Over 12 years ago by Henning Koch.

When selecting records in a date range, take care not to do it like this: start_date = Date.parse('2007-05...

Use Time.current / Date.current / DateTime.current on projects that have a time zone

Posted Over 12 years ago by Arne Hartherz.

Basically, you now need to know if your project uses a "real" time zone or :local, and if config.active_record.time_zone...

How to use html_safe correctly

Posted Over 12 years ago by Henning Koch.

Back in the war, Rails developers had to manually HTML-escape user-supplied text before it was rendered in a...

Invoices: How to properly round and calculate totals

Posted Over 12 years ago by Henning Koch.

While it might seem trivial to implement an invoice that sums up items and shows net, gross and vat totals...

Detect the current Rails environment from JavaScript or CSS

Posted Over 12 years ago by Henning Koch.

Detecting if a Javascript is running under Selenium WebDriver is super-painful. It's much easier to detect the current...

How to grep through the DOM using the Capybara API

Posted Almost 13 years ago by Henning Koch.

When your Cucumber feature needs to browse the page HTML, and you are not sure how to express your query...

Rails I18n fallback locales

Posted Almost 13 years ago by Henning Koch.

When you need to create a locale for a language variant (like Austrian for German), you probably don't want...

Configuring ActionMailer host and protocol for URL generation

Posted Almost 13 years ago by Henning Koch.

When you generate a URL in a mailer view, ActionMailer will raise an error unless you previously configured it which...

Popular mistakes when using nested forms

Posted Almost 13 years ago by Henning Koch.

Here are some popular mistakes when using nested forms: You are using fields_for instead of form.fields_for.

How to use Ubuntu in English, but still show German formats

Posted Almost 13 years ago by Henning Koch.

If you want to have an English Ubuntu UI, but still see dates, money amounts, paper formats, etc. in German...

Merging two arbitrary ActiveRecord scopes

Posted Almost 13 years ago by Henning Koch.

(Rails has a method ActiveRecord::Relation#merge that can merge ActiveRecord scopes. However, its behavior has never been clear, and...

Git: Advisory for cherry-picks to production branches

Posted Almost 13 years ago by Tobias Kraze.

We often have a separate production branch that lags a bit behind the more cutting edge main branch. Sometimes you...

Use find_in_batches or find_each to deal with many records efficiently

Posted Almost 13 years ago by Arne Hartherz.

Occasionally you need to do something directly on the server -- like having all records recalculate something that cannot be done...

How DECIMAL columns deal with numbers exceeding their precision or scale

Posted About 13 years ago by Henning Koch.

When storing floating-point numbers such as prices or totals in an SQL database, always use a DECIMAL column. Never...

Cancelling event propagation

Posted About 13 years ago by Tobias Kraze.

Within an event handler, there are multiple methods to cancel event propagation, each with different semantics. event.preventDefault() Only prevents the...

ActiveRecord: Creating many records works faster in a transaction

Posted About 13 years ago by Henning Koch.

When you need to insert many records into the same table, performance may become an issue. What you can do...

Why two Ruby Time objects are not equal, although they appear to be

Posted About 13 years ago by Henning Koch.

So you are comparing two Time objects in an RSpec example, and they are not equal, although they look equal...

Defining class methods with Modularity traits

Posted About 13 years ago by Henning Koch.

There are two ways to define a class method from a Modularity trait. Note that the usual caveats regarding class...

Testing if two date ranges overlap in Ruby or Rails

Posted About 13 years ago by Henning Koch.

A check if two date or time ranges A and B overlap needs to cover a lot of cases:

How to define constants with traits

Posted About 13 years ago by Arne Hartherz.

When defining a trait using the Modularity gem, you must take extra steps to define constants to avoid caveats (like...