Home > Back-end >  ActiveRecord::RecordNotFound at Active Storage redirect
ActiveRecord::RecordNotFound at Active Storage redirect

Time:12-12

My application uses Active Storage to manage images (attached to articles, user avatars, etc) and I get constant 500 errors when processing links of the following style:

/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBODYwR1E9PSIsImV4cCI6bnVsbCwicHVyIjoiYmxvYl9pZCJ9fQ==--ac33ecf9f5dffc2b8345b443c402562e4fb6afaf/F582BAA7-DD39-4A7A-8E82-919EC8AFC12C.jpeg

These links are sometimes visited by search engine bots, and sometimes because other websites point to images of my application that no longer exist.

To generate the links to the images I use the helper recommended by Rails, which point to the assets through a redirect (url_for(element.attachment) for example).

The thing is that if the assets no longer exist, because a user has changed avatar for example, or because in an article an image has been replaced, these links generate errors with status 500 instead of 404 errors.

Am I confused and I am not using the right helper to generate the image paths? I have investigated in the Ruby Gem and I don't see any option to change the behavior, since the error is given when looking for the ActiveRecord of the blob in database. This is the trace:

vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/core.rb:284:in `find': Couldn't find ActiveStorage::Blob with 'id'=1684686 (ActiveRecord::RecordNotFound)
    from vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/signed_id.rb:64:in `find_signed!'
    from vendor/bundle/ruby/3.1.0/gems/activestorage-7.0.4/app/models/active_storage/blob.rb:87:in `find_signed!'
    from vendor/bundle/ruby/3.1.0/gems/activestorage-7.0.4/app/controllers/concerns/active_storage/set_blob.rb:12:in `set_blob'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:400:in `block in make_lambda'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:199:in `block (2 levels) in halting'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/abstract_controller/callbacks.rb:34:in `block (2 levels) in <module:Callbacks>'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:200:in `block in halting'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:595:in `block in invoke_before'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:595:in `each'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:595:in `invoke_before'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:116:in `block in run_callbacks'
    from vendor/bundle/ruby/3.1.0/gems/actiontext-7.0.4/lib/action_text/rendering.rb:20:in `with_renderer'
    from vendor/bundle/ruby/3.1.0/gems/actiontext-7.0.4/lib/action_text/engine.rb:69:in `block (4 levels) in <class:Engine>'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:127:in `instance_exec'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:127:in `block in run_callbacks'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:138:in `run_callbacks'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/abstract_controller/callbacks.rb:233:in `process_action'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_controller/metal/rescue.rb:22:in `process_action'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_controller/metal/instrumentation.rb:67:in `block in process_action'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/notifications.rb:206:in `block in instrument'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/notifications.rb:206:in `instrument'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_controller/metal/instrumentation.rb:66:in `process_action'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_controller/metal/params_wrapper.rb:259:in `process_action'
    from vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/railties/controller_runtime.rb:27:in `process_action'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/abstract_controller/base.rb:151:in `process'
    from vendor/bundle/ruby/3.1.0/gems/actionview-7.0.4/lib/action_view/rendering.rb:39:in `process'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_controller/metal.rb:188:in `dispatch'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_controller/metal.rb:251:in `dispatch'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/routing/route_set.rb:32:in `serve'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/journey/router.rb:50:in `block in serve'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/journey/router.rb:32:in `each'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/journey/router.rb:32:in `serve'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/routing/route_set.rb:852:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/rack/agent_hooks.rb:30:in `traced_call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/rack/browser_monitoring.rb:38:in `traced_call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/http_accept_language-2.1.1/lib/http_accept_language/middleware.rb:14:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/tempfile_reaper.rb:15:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/etag.rb:27:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/conditional_get.rb:27:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/head.rb:12:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/http/permissions_policy.rb:38:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/http/content_security_policy.rb:36:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/session/abstract/id.rb:266:in `context'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/session/abstract/id.rb:260:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/cookies.rb:696:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/callbacks.rb:99:in `run_callbacks'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/callbacks.rb:26:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/bugsnag-6.25.0/lib/bugsnag/integrations/rack.rb:51:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/debug_exceptions.rb:28:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/show_exceptions.rb:26:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/rack/logger.rb:40:in `call_app'
    from vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/rack/logger.rb:25:in `block in call'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/tagged_logging.rb:99:in `block in tagged'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/tagged_logging.rb:37:in `tagged'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/tagged_logging.rb:99:in `tagged'
    from vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/rack/logger.rb:25:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/remote_ip.rb:93:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/request_store-1.5.1/lib/request_store/middleware.rb:19:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/request_id.rb:26:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/method_override.rb:24:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/runtime.rb:22:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/executor.rb:14:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/static.rb:23:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/sendfile.rb:110:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/ssl.rb:77:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.4/lib/action_dispatch/middleware/host_authorization.rb:131:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-utf8_sanitizer-1.8.0/lib/rack/utf8_sanitizer.rb:28:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/rack-cors-1.1.1/lib/rack/cors.rb:100:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/railties-7.0.4/lib/rails/engine.rb:530:in `call'
    from vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.13.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:99:in `call'
    from vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/configuration.rb:268:in `call'
    from vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/request.rb:89:in `block in handle_request'
    from vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/thread_pool.rb:343:in `with_force_shutdown'
    from vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/request.rb:88:in `handle_request'
    from vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/server.rb:430:in `process_client'
    from vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/server.rb:233:in `block in run'
    from vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/thread_pool.rb:150:in `block in spawn_thread'

Do you have any suggestion to make a better use of Active Storage? Do you avoid the error in any way in your applications?

Thank you very much in advance!

CodePudding user response:

Check if this solves your issue:

Rails.application.routes.url_helpers.polymorphic_url(user.avatar)

CodePudding user response:

I just realized that I am confusing a "500 status error" with an INFO level exception. The application responds fine: it displays a 404 error, but my error handler app gets an INFO level exception and I was interpreting it as my users being shown an error screen, when really the application was handling the situation fine.

  • Related