Rails 5.0 and 5.1 Upgrade Nuances

Just a few notes to remind me of oddities if I’m ever involved in another Rails upgrade to 5.0, 5.1, 5.2, etc.

Deep Munging change in Rails 5.0

Basically, for a long time, Rails did odd things with JSON parsing. This was finally fixed here https://github.com/rails/rails/pull/16924, but your application could have code that depends on the odd behavior.

Example:

Rails 4: { “posts_to_delete_ids”: [] } resulted in { “posts_to_delete_ids” => nil }
Rails 5: { “posts_to_delete_ids”: [] } resulted in { “posts_to_delete_ids” => [] }

This can be tough to catch with controller specs because you might inadvertently have passed in params differently than how Rails actually would parse a JSON HTTP POST.

Undocumented Change to persistence behavior between 5.0 and 5.1

In after_save, after_update callbacks, the behavior of resaving a record changed a bit between 5.0 and 5.1, under certain circumstances:

Specifically, this is true if you flip a value back to its original value before the original save.

in Rails 5.0, changing/saving a record in an after callback did not actually persist the change.
In Rails 5.1, it does persist the change.

So suppose you have a User with name Alpha.then suppose you do this:

1. user.update!(name: “Bravo”)
2. Then, in an after_update callback, you run user.update!(name: “Alpha”).
When it tries to save, it thinks the name changed from Alpha to Alpha instead of Bravo to Alpha, so it doesn’t actually save anything.

Improperly documented between changed between 5.0 and 5.1 on return false in a before callback

There is an improperly documented change between 5.0 to 5.1 on how returning false in a before callback halts the callback chain. On 5.1, there is a message claiming this will be removed in 5.2, but it was actually removed in 5.1. Basically the deprecation messages weren’t adjusted in the correct version.