NoMethodError: undefined method `halt_callback_chains_on_return_false=' for ActiveSupport:Module

三世轮回 提交于 2019-12-01 15:25:10

问题


I've been developing this RoR 5.1 application for a while, and I need to add a new migration now:

class AddActiveFlagToParameters < ActiveRecord::Migration[5.1]
  def change
    add_column :parameters, :is_active, :boolean, :default => true 
  end
end

When I try to run the migration, rails raises the error:

NoMethodError: undefined method `halt_callback_chains_on_return_false=' for ActiveSupport:Module

Reading around, I finally worked around the issue by upgrading to Rails 5.2 (gem activesupport 5.2.0) and commenting out the line in the file config/initializers/new_framework_defaults.rb

But this sounds like a short term solution.

Where does this come from? How can I safely handle this issue ?


回答1:


halt_callback_chains_on_return_false setting in the initializer was a solution for temporary keeping old callback behaviour after upgrade to Rails 5.0. Assumed that you need time to check all callbacks in the app and after it you can remove this setting. And assumed that on the upgrade to 5.2 all is already checked, so this setting is removed.

Before Rails 5, returning false from any before_ callback in ActiveModel or ActiveModel::Validations, ActiveRecord and ActiveSupport resulted in halting of callback chain.

Starting from Rails 5.0 if any before_ callback returns false then callback chain is not halted. To explicitly halt the callback chain, we need to use throw(:abort).

So you need to check all before_callbacks in the app for proper behaviour, change them if needed and remove this line from initializer after it.

You can read more here



来源:https://stackoverflow.com/questions/49744200/nomethoderror-undefined-method-halt-callback-chains-on-return-false-for-acti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!