I'm migrating from Rails 4.1 to Rails 5.1 and got the following deprecation warning:
DEPRECATION WARNING: Returning false in Active Record and Active Model callbacks will not implicitly halt a callback chain in Rails 5.1. To explicitly halt the callback chain, please use throw :abort instead.
I added throw(:abort) in callbacks that rely on returning false to halt the callback chain. When I run tests, I got a lot of errors like the following:
Error:
UserControllerTest#test_: the update action should have updated user record. :
UncaughtThrowError: uncaught throw :abort
app/controllers/application_controller.rb:298:in `throw'
app/controllers/application_controller.rb:298:in `check_access'
I'm a bit confused. Since Rails 5 asks people to use throw :abort in callbacks, why the UncaughtThrowError?
Am currently at Rails 5.1.7.
Any help is appreciated. Thanks.
Related
I have recently upgraded from Rails 4.2 to 5.0. I know about the change in callback halting using throw(:abort) instead of returning false. My problem is that I can't make the deprecation warnings go away.
DEPRECATION WARNING: Returning false in Active Record and Active Model callbacks will not implicitly halt a callback chain in Rails 5.1. To explicitly halt the callback chain, please use throw :abort instead.
I have made config/initializers/callback_terminator.rb file with following code
ActiveSupport.halt_callback_chains_on_return_false = false
but I am not still not able to get rid of the warning. Nor am I getting the expected behaviour. It seems that this configuration is not being applied.
Is there something I am missing?
Put the config in after config.after_initialize block in application.rb file like this.
config.after_initialize do
ActiveSupport.halt_callback_chains_on_return_false = false
end
Rails 4.2.3, IdentityCache gem 0.2.5
I am calling ZipCodeLookup.fetch_by_id(value) and I am getting the following error:
undefined method `fetch_value' for #<Hash:0x007fbc6d261e08>
I have the following attributes in my ZipCodeLookup model
include IdentityCache
cache_index :zip_code, unique: true
I haven't seen anyone else with this issue. Does anyone know how to fix this?
fyi works in Rails 4.1.x
the error came from not clearing the cache. so run the following command in the rails console: IdentityCache.cache.clear
I've seen the same exception before without using IdentityCache. In my case, some ActiveRecord models were manually marshaled via Marshal::dump and saved as binary stream. Then, Ruby and Rails were upgraded.
Afterwards, calling Marshal::load on the marshaled copies would retrieve them and object.class would show the right model's name, but accessing any attribute within would throw the same exception.
I had to clear the marshaled copies and generate new ones.
Upgrading to Rails 4.2, everything working fine locally, push to production 'kaboom'. Every Delayed::Job failed with the same error.
Job failed to load: undefined method 'fetch_value' for #. Handler: " --- YAMLYAMLYAMLYAML "
The error message we're getting is coming from the function below, which is calling into different libraries, catching the actual error, and returning their own which makes it really difficult to debug.
def payload_object
#payload_object ||= YAML.load_dj(handler)
rescue TypeError, LoadError, NameError, ArgumentError, SyntaxError, Psych::SyntaxError => e
raise DeserializationError, "Job failed to load: #{e.message}. Handler: #{handler.inspect}"
end
Other things that might be useful, we're only using Delayed::Job (not with Active::Job).
fetch_value is never called within delayed_job, it's an Active::Record method.
I came across a similar fetch_value error when I upgraded an app to Rails 4.2.1. To get a closer understanding of the error, grab any job with this error and run the following in the rails console:
YAML.load_dj(Delayed::Job.find(JOB_ID).handler)
It runs internal delayed job code, but should give you a better trace for debugging.
source: https://github.com/collectiveidea/delayed_job/blob/master/lib/delayed/backend/base.rb#L90
The error is likely the result of queueing/creating jobs pre-Rails 4.2 upgrade and before setting config.active_job.queue_adapter = :delayed_job in your application.rb. If the config was not properly set, then jobs would be serialized in a format that Rails 4.2 cannot handle. To run any jobs with the fetch_value error, you can switch to a branch of your code still on > Rails 4.2 and run the jobs from the rails console.
You can try to update delayed_job to newer version, there was a series of changes for rails 4/4.2/4.4 support, and see if it brakes things in your app.
And for rails 4.2 they expect you set config.active_job.queue_adapter = :delayed_job in config/application.rb
My Rails 4/Ruby 2 app is throwing the following warning every time my RSpec tests create a FactoryGirl object: "DEPRECATION WARNING: This dynamic method is deprecated. Please use e.g. Post.find_or_create_by(name: 'foo') instead."
This warning is not thrown when I run my app in development. Is FactoryGirl's code throwing this? I tried to find some information but it doesn't look like other people are getting this.
If you tell Rails to give you a full stack trace for the deprecation warning, you should be able to diagnose it pretty easily. The warnings are coming from a library called ActiveSupport::Deprecation - tell it to run in debug mode.
# config/environments/test.rb
ActiveSupport::Deprecation.debug = true
For me, the warnings were caused by an old version of the Stringex library.
FactoryGirl would make a new model, which would trigger a call to one of the Stringex methods, which would raise the warning, although there was no way to see that until I turned on full stack traces. bundle update stringex solved the issue with no problem.
It looks like it's coming from ActiveRecord.
module DeprecationWarning
def body
"#{deprecation_warning}\n#{super}"
end
def deprecation_warning
%{ActiveSupport::Deprecation.warn("This dynamic method is deprecated. Please use e.g. #{deprecation_alternative} instead.")}
end
end
I'm not sure why you're not getting the warnings in development. Is your environment suppressing the warnings?
Where can I register a handler in Rails 3 that can catch view errors? There is a class of error that Rails raises that we wish to handle and silence instead of logging as FATAL.
(These are not errors in our code, rather errors caused by the client closing the connection before the page has finished rendering, and Rails/unicorn attempting to write to a broken pipe.)
Will rescue_from work for you? http://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html
You can conceivably rescue (and silence) your particular subclass of ActionView::TemplateError