Active Record bug fixes, faster Date#advance, and more!

Posted by eugeneius, January 28, 2019 @ 12:00 am in News

Hello everyone! This is Eugene, with a selection of interesting improvements to Rails merged in the past week.

Don’t load app environment when editing credentials

Support for per-environment credentials is coming in Rails 6.0, but adding a new environment wasn’t always easy: if your application accessed credentials during boot, the credentials:edit command would fail to run, which would in turn prevent you from adding the missing credentials. Oops!

All queries should return correct results even if they include large numbers

When prepared statements are enabled, querying an integer column with an out-of-range value causes an error in the underlying database driver. To work around this issue, Active Record used to refuse to run the query and always return a negative result, e.g. ActiveRecord::RecordNotFound.

This approach worked for simple queries, but gave incorrect results for more complex ones. Out-of-range values are now converted to “impossible” predicates instead (e.g. WHERE 1=0), which allows the query to run without an error while still producing the correct result.

Speaking of large numbers: this pull request, opened 18 months ago, was number 30,000 on the Rails repository. 🎈

Fix year value when casting a multi-parameter time hash

Values stored in TIME columns in the database are mapped to Time objects in Ruby. The latter includes date parts, where the former doesn’t; to deal with this discrepancy, a default date of January 1st, 2000 is always assigned.

When a multi-parameter time value—as generated by the time_select helper—was assigned to an attribute, its year would be set to 1970 instead, producing a different result depending on where the value came from.

With this fix, time attributes have fully joined us in the 21st century. 🖖

Faster and more memory-efficient Date#advance

This method was copying its options hash unnecessarily. Now that it doesn’t, it’s both faster and allocates less memory.

28 people contributed to Rails since our last issue; you can see the full list of changes here.

Until next week!