Upgrading rails apps to 5.x
Montag, 09. Juli 2018, 09:23 Uhr | roberto@vasquez-angel.de |Changed behaviour for dirty attributes:
# rails < 5
after_update do
if self.active_changed?
# ...
end
end
# rails 5
after_update do
if saved_change_to_active?
# ...
end
end
Changed the way to clear has_many associations:
# rails < 5
self.comments = []
# rails 5
self.comments.clear
Changed the way to make a has_many association unique:
This one is tricky as the exception happens inside ActiveRecord:
[1] pry(#<Catalog::OldProduct>)> comments.clear
NoMethodError: undefined method `extensions' for []:Array
Did you mean? extend
from /xxx@example_app/gems/activerecord-5.2.0/lib/active_record/associations/association.rb:134:in `extensions'
Solution:
# rails < 5
has_many :comments, -> { uniq }
# rails 5
has_many :comments, -> { distinct }
But when trying to clear the association you are presented another exception:
comments.clear
ActiveRecord::ActiveRecordError: delete_all doesn't support distinct
from /xxx@example_app/gems/activerecord-5.2.0/lib/active_record/relation.rb:384:in `delete_all'
Solution:
# rails < 5
comments.clear
# rails 5
comments.all.each(&:destroy)
The way params are passed in RSpec controller specs has changed in newer RSpec versions:
# old syntax
get :find_by_uuid, :format => :json, :uuid => 'some-uuid-1234'
# new syntax
get :find_by_uuid, format: :json, params: { uuid: 'some-uuid-1234' }