Skip to content

Deprecation warning in Rails 5.1 #1165

@afpr252

Description

@afpr252

Hi. We're using the latest version of the thinking sphinx gem (4.4.1) and we recently upgraded to Rails 5.1. Since that upgrade, we've been seeing deprecation warnings in the gem.

Problem

These are the logs we have:

  • DEPRECATION WARNING: The behavior of 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_changes? instead.

  • 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.

The code that is calling this deprecated method is on gems/thinking-sphinx-4.4.1/lib/thinking_sphinx/active_record/callbacks/delta_callbacks.rb#new_or_changed?

  def new_or_changed?
    instance.new_record? || instance.changed?
  end

The deprecated method is https://apidock.com/rails/ActiveModel/Dirty/changed%3F and the new method that replaces it is https://apidock.com/rails/ActiveRecord/AttributeMethods/Dirty/saved_changes%3F.

Steps to reproduce the issue

The way we reproduced the issue was to create a callback in a model that issues an update which then triggers the thinking sphinx gem.

class User < ApplicationModel
  ...
  # this will trigger the affected code inside a ActiveRecord callback
  after_update{ update!(some_attribute: nil) }
end

ThinkingSphinx::Index.define :user, with: :active_record, delta: ThinkingSphinx::Deltas::SidekiqDelta do
  ...
end

Then open a rails console and:

user = User.last
user.update!(some_other_attribute: nil)

Interim solution

As expected, we managed to solve the deprecation warning by monkey-patching the affected class:

class ThinkingSphinx::ActiveRecord::Callbacks::DeltaCallbacks
  private

  def new_or_changed?
    instance.new_record? || instance.saved_changes?
  end
end

However, we would rather to have the fix in upstream.

Do you want me to open a PR with the way we fixed the deprecation warning or is there a better solution given that this new method is not available on Rails prior to 5.1?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions