Rails after callbacks changes

I was working on Rails upgrade from 5.0.6 to 5.1.4 today.

Lots of deprecation warnings showed on CI build like:

DEPRECATION WARNING: The behavior of `attribute_was` inside of after callbacks
will be changing in the next version of Rails. The new return value will reflect
the behavior of calling the method after `save` returned (e.g. the opposite of
what it returns now). To maintain the current behavior, use
`attribute_before_last_save` instead. (called from xxx at /home/circleci/my-app/app/models/xxx.rb:218)

DEPRECATION WARNING: The behavior of `attribute_changed?` inside of after
callbacks will be changing in the next version of Rails. The new return
value will reflect the behavior of calling the method after `save` returned
(e.g. the opposite of what it returns now). To maintain the current behavior,
use `saved_change_to_attribute?` instead. (called from xxx at /home/circleci/my-app/app/models/xxx.rb:218)

DEPRECATION WARNING: The behavior of `changed_attributes` inside of after
callbacks will be changing in the next version of Rails. The new return
value will reflect the behavior of calling the method after `save` returned
(e.g. the opposite of what it returns now). To maintain the current behavior,
use `saved_changes.transform_values(&:first)` instead. (called from xxx at /home/circleci/my-app/app/models/xxx.rb:218)

(Indented for readability)

From Rails 5.2, some methods behave differently in after callbacks.

I changed the methods before upgrading to 5.2 because I dislike lots of dirty warnings.

The changes are:

  • <attribute>_was -> <attribute>_before_last_save
  • <attribute>_changed? -> saved_change_to_<attribute>?
  • changed_attributes -> saved_changes.transform_values(&:first) (sucks)
  • changes -> saved_changes.keys
  • changed? -> saved_changes?

When they are in the ActiveRecord’s after_ callbacks, you can find them easily.

But it irritated me a little bit that they are called in controller or listener(with whisper gem)

You have to be careful if the above deprecated methods are NOT CALLED after_ callbacks.

Some of them must be changed as follows if it’s called before_ callbacks,

  • <attribute>_was -> <attribute>_change_to_be_saved
  • <attribute>_changed? -> will_save_change_to_<attribute>?

In addition, I changed following things:

:headers option in RSpec’s controller test was removed

# Not work
get :index, params: { page: 2 }, headers: { "SOME_TOKEN" => "XXX" }

# Works
request.headers.merge!("SOME_TOKEN" => "XXX")
get :index, params: { page: 2 }

from this information

Thanks!

ActiveRecord_Relation is now private constant

This is not a problem in most cases.

I had written bad codes in test. (and still not good)

# Bad
assert_equal Post::ActiveRecord_Relation, assigns(:posts)

# Works (But not Good)
assert assigns(:posts).all? { |post| post.is_a?(Post) }

AC::Parameters#to_h raises an error when it is not permitted

In helpers, params.to_query is directly called and it raises error, then changes to params.permit!.to_query

(You should be careful to use permit!).

Video Games

About latest my game life.

  • Less intereted in Splatoon2 :thought_balloon:
  • Bought a gaming PC for PUBG
  • Bought some PC games:
    • PUBG
    • Fallout: New Vegas
    • Borderlands2
  • Hunger for Xenoblade2

I play Fallout in English, it was really hard… :innocent:

Contents