Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActsAsTenant unexpectedly blocks generation of ActiveStorage representation #330

Open
ConfusedVorlon opened this issue Jan 2, 2024 · 8 comments

Comments

@ConfusedVorlon
Copy link

ConfusedVorlon commented Jan 2, 2024

Firstly - thanks for a gem of a gem :)

I have

config.require_tenant = true

My test model has an active storage attachment:

class Test < ApplicationRecord
  acts_as_tenant :account

  has_one_attached :video
end

I should be able to view a representation for this blob using:

    <% blob = test.video.blob %>
    <% if blob.representable? %>
      <%= image_tag url_for(blob.representation(resize_to_limit: [ 800, 600 ] )) %>
    <% end %>

This generates a link to the representations redirect controller. In my case:

http://localhost:3000/rails/active_storage/representations/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--bb17c469cc67487b1cd754e16f430df53d63f650/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCam9VY21WemFYcGxYM1J2WDJ4cGJXbDBXd2RwQWdBRWFRSUFBdz09IiwiZXhwIjpudWxsLCJwdXIiOiJ2YXJpYXRpb24ifX0=--b0ad9c958551b563ac36c2b25326c7e8112efc91/1min.mp4

This is a rails controller accessing an active storage attachment (which isn't scoped to an account), so I wouldn't expect it to trigger an ActsAsTenant error - however, I get

ActsAsTenant::Errors::NoTenantSet in ActiveStorage::Representations::RedirectController#show

Full stack is copied below. I think this is triggered by the attempt to touch attachment records after creating a variant

Technically, I suppose this is a correct catch by ActsAsTenant, but practically speaking, it's not what I would expect to happen, and an unexpected breakage in Rails behaviour.

I have a simple demonstration project if that would help.

Thoughts?

Full Stack ` 19:20:11 web.1 | ActsAsTenant::Errors::NoTenantSet (ActsAsTenant::Errors::NoTenantSet): 19:20:11 web.1 | 19:20:11 web.1 | acts_as_tenant (1.0.1) lib/acts_as_tenant/model_extensions.rb:21:in `block in acts_as_tenant' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/scoping/default.rb:163:in `instance_exec' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/scoping/default.rb:163:in `block (2 levels) in build_default_scope' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/scoping/default.rb:159:in `each' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/scoping/default.rb:159:in `inject' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/scoping/default.rb:159:in `block in build_default_scope' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/scoping/default.rb:197:in `evaluate_default_scope' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/scoping/default.rb:158:in `build_default_scope' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/scoping/named.rb:46:in `default_scoped' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/scoping/named.rb:40:in `scope_for_association' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/associations/preloader/association.rb:291:in `build_scope' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/associations/preloader/association.rb:182:in `scope' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/associations/preloader/association.rb:163:in `loader_query' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/associations/preloader/batch.rb:41:in `each' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/associations/preloader/batch.rb:41:in `group_by' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/associations/preloader/batch.rb:41:in `group_and_load_similar' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/associations/preloader/batch.rb:27:in `call' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/associations/preloader.rb:121:in `call' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/relation.rb:867:in `block in preload_associations' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/relation.rb:866:in `each' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/relation.rb:866:in `preload_associations' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/relation.rb:951:in `block in exec_queries' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/relation.rb:1003:in `skip_query_cache_if_necessary' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/relation.rb:941:in `exec_queries' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/association_relation.rb:44:in `exec_queries' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/relation.rb:727:in `load' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/relation.rb:264:in `records' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/relation/delegation.rb:100:in `each' 19:20:11 web.1 | activestorage (7.1.2) app/models/active_storage/blob.rb:380:in `touch_attachment_records' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:403:in `block in make_lambda' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:239:in `block in halting_and_conditional' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:602:in `block in invoke_after' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:602:in `each' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:602:in `invoke_after' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:111:in `run_callbacks' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:952:in `_run_update_callbacks' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/callbacks.rb:449:in `_update_record' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/timestamp.rb:120:in `_update_record' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/persistence.rb:1220:in `create_or_update' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/callbacks.rb:441:in `block in create_or_update' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:121:in `block in run_callbacks' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/autosave_association.rb:375:in `around_save_collection_association' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:130:in `block in run_callbacks' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:141:in `run_callbacks' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:952:in `_run_save_callbacks' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/callbacks.rb:441:in `create_or_update' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/timestamp.rb:125:in `create_or_update' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/persistence.rb:718:in `save' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/validations.rb:49:in `save' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/transactions.rb:309:in `block in save' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/transactions.rb:365:in `block in with_transaction_returning_status' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/connection_adapters/abstract/transaction.rb:535:in `block in within_new_transaction' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/concurrency/null_lock.rb:9:in `synchronize' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/connection_adapters/abstract/transaction.rb:532:in `within_new_transaction' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/connection_adapters/abstract/database_statements.rb:344:in `transaction' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/transactions.rb:361:in `with_transaction_returning_status' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/transactions.rb:309:in `save' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/suppressor.rb:52:in `save' 19:20:11 web.1 | activestorage (7.1.2) lib/active_storage/attached/one.rb:61:in `attach' 19:20:11 web.1 | activestorage (7.1.2) app/models/active_storage/preview.rb:101:in `block (2 levels) in process' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/connection_handling.rb:361:in `with_role_and_shard' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/connection_handling.rb:147:in `connected_to' 19:20:11 web.1 | activestorage (7.1.2) app/models/active_storage/preview.rb:100:in `block in process' 19:20:11 web.1 | activestorage (7.1.2) lib/active_storage/previewer/video_previewer.rb:26:in `block (2 levels) in preview' 19:20:11 web.1 | activestorage (7.1.2) lib/active_storage/previewer.rb:55:in `block in draw' 19:20:11 web.1 | activestorage (7.1.2) lib/active_storage/previewer.rb:63:in `open_tempfile' 19:20:11 web.1 | activestorage (7.1.2) lib/active_storage/previewer.rb:50:in `draw' 19:20:11 web.1 | activestorage (7.1.2) lib/active_storage/previewer/video_previewer.rb:33:in `draw_relevant_frame_from' 19:20:11 web.1 | activestorage (7.1.2) lib/active_storage/previewer/video_previewer.rb:25:in `block in preview' 19:20:11 web.1 | activestorage (7.1.2) lib/active_storage/downloader.rb:15:in `block in open' 19:20:11 web.1 | activestorage (7.1.2) lib/active_storage/downloader.rb:24:in `open_tempfile' 19:20:11 web.1 | activestorage (7.1.2) lib/active_storage/downloader.rb:12:in `open' 19:20:11 web.1 | activestorage (7.1.2) lib/active_storage/service.rb:92:in `open' 19:20:11 web.1 | activestorage (7.1.2) app/models/active_storage/blob.rb:298:in `open' 19:20:11 web.1 | activestorage (7.1.2) lib/active_storage/previewer.rb:32:in `download_blob_to_tempfile' 19:20:11 web.1 | activestorage (7.1.2) lib/active_storage/previewer/video_previewer.rb:24:in `preview' 19:20:11 web.1 | activestorage (7.1.2) app/models/active_storage/preview.rb:99:in `process' 19:20:11 web.1 | activestorage (7.1.2) app/models/active_storage/preview.rb:49:in `processed' 19:20:11 web.1 | activestorage (7.1.2) app/controllers/active_storage/representations/base_controller.rb:14:in `set_representation' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:403:in `block in make_lambda' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:202:in `block (2 levels) in halting' 19:20:11 web.1 | actionpack (7.1.2) lib/abstract_controller/callbacks.rb:34:in `block (2 levels) in ' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:203:in `block in halting' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:598:in `block in invoke_before' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:598:in `each' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:598:in `invoke_before' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:119:in `block in run_callbacks' 19:20:11 web.1 | actiontext (7.1.2) lib/action_text/rendering.rb:23:in `with_renderer' 19:20:11 web.1 | actiontext (7.1.2) lib/action_text/engine.rb:69:in `block (4 levels) in ' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:130:in `instance_exec' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:130:in `block in run_callbacks' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:141:in `run_callbacks' 19:20:11 web.1 | actionpack (7.1.2) lib/abstract_controller/callbacks.rb:258:in `process_action' 19:20:11 web.1 | actionpack (7.1.2) lib/action_controller/metal/rescue.rb:25:in `process_action' 19:20:11 web.1 | actionpack (7.1.2) lib/action_controller/metal/instrumentation.rb:74:in `block in process_action' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/notifications.rb:206:in `block in instrument' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/notifications/instrumenter.rb:58:in `instrument' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/notifications.rb:206:in `instrument' 19:20:11 web.1 | actionpack (7.1.2) lib/action_controller/metal/instrumentation.rb:73:in `process_action' 19:20:11 web.1 | actionpack (7.1.2) lib/action_controller/metal/params_wrapper.rb:261:in `process_action' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/railties/controller_runtime.rb:32:in `process_action' 19:20:11 web.1 | actionpack (7.1.2) lib/abstract_controller/base.rb:160:in `process' 19:20:11 web.1 | actionview (7.1.2) lib/action_view/rendering.rb:40:in `process' 19:20:11 web.1 | actionpack (7.1.2) lib/action_controller/metal.rb:227:in `dispatch' 19:20:11 web.1 | actionpack (7.1.2) lib/action_controller/metal.rb:309:in `dispatch' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/routing/route_set.rb:49:in `dispatch' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/routing/route_set.rb:32:in `serve' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/journey/router.rb:51:in `block in serve' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/journey/router.rb:131:in `block in find_routes' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/journey/router.rb:124:in `each' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/journey/router.rb:124:in `find_routes' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/journey/router.rb:32:in `serve' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/routing/route_set.rb:882:in `call' 19:20:11 web.1 | rack (3.0.8) lib/rack/tempfile_reaper.rb:20:in `call' 19:20:11 web.1 | rack (3.0.8) lib/rack/etag.rb:29:in `call' 19:20:11 web.1 | rack (3.0.8) lib/rack/conditional_get.rb:31:in `call' 19:20:11 web.1 | rack (3.0.8) lib/rack/head.rb:15:in `call' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/http/permissions_policy.rb:36:in `call' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/http/content_security_policy.rb:33:in `call' 19:20:11 web.1 | rack-session (2.0.0) lib/rack/session/abstract/id.rb:272:in `context' 19:20:11 web.1 | rack-session (2.0.0) lib/rack/session/abstract/id.rb:266:in `call' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/middleware/cookies.rb:689:in `call' 19:20:11 web.1 | activerecord (7.1.2) lib/active_record/migration.rb:654:in `call' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/callbacks.rb:101:in `run_callbacks' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/middleware/callbacks.rb:28:in `call' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/middleware/executor.rb:14:in `call' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/middleware/actionable_exceptions.rb:16:in `call' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/middleware/debug_exceptions.rb:29:in `call' 19:20:11 web.1 | web-console (4.2.1) lib/web_console/middleware.rb:132:in `call_app' 19:20:11 web.1 | web-console (4.2.1) lib/web_console/middleware.rb:28:in `block in call' 19:20:11 web.1 | web-console (4.2.1) lib/web_console/middleware.rb:17:in `catch' 19:20:11 web.1 | web-console (4.2.1) lib/web_console/middleware.rb:17:in `call' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call' 19:20:11 web.1 | railties (7.1.2) lib/rails/rack/logger.rb:37:in `call_app' 19:20:11 web.1 | railties (7.1.2) lib/rails/rack/logger.rb:24:in `block in call' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/tagged_logging.rb:135:in `block in tagged' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/tagged_logging.rb:39:in `tagged' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/tagged_logging.rb:135:in `tagged' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/broadcast_logger.rb:240:in `method_missing' 19:20:11 web.1 | railties (7.1.2) lib/rails/rack/logger.rb:24:in `call' 19:20:11 web.1 | sprockets-rails (3.4.2) lib/sprockets/rails/quiet_assets.rb:13:in `call' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/middleware/remote_ip.rb:92:in `call' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/middleware/request_id.rb:28:in `call' 19:20:11 web.1 | rack (3.0.8) lib/rack/method_override.rb:28:in `call' 19:20:11 web.1 | rack (3.0.8) lib/rack/runtime.rb:24:in `call' 19:20:11 web.1 | activesupport (7.1.2) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/middleware/server_timing.rb:59:in `block in call' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/middleware/server_timing.rb:24:in `collect_events' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/middleware/server_timing.rb:58:in `call' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/middleware/executor.rb:14:in `call' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/middleware/static.rb:25:in `call' 19:20:11 web.1 | rack (3.0.8) lib/rack/sendfile.rb:114:in `call' 19:20:11 web.1 | actionpack (7.1.2) lib/action_dispatch/middleware/host_authorization.rb:141:in `call' 19:20:11 web.1 | railties (7.1.2) lib/rails/engine.rb:529:in `call' 19:20:11 web.1 | puma (6.4.0) lib/puma/configuration.rb:272:in `call' 19:20:11 web.1 | puma (6.4.0) lib/puma/request.rb:100:in `block in handle_request' 19:20:11 web.1 | puma (6.4.0) lib/puma/thread_pool.rb:378:in `with_force_shutdown' 19:20:11 web.1 | puma (6.4.0) lib/puma/request.rb:99:in `handle_request' 19:20:11 web.1 | puma (6.4.0) lib/puma/server.rb:443:in `process_client' 19:20:11 web.1 | puma (6.4.0) lib/puma/server.rb:241:in `block in run' 19:20:11 web.1 | puma (6.4.0) lib/puma/thread_pool.rb:155:in `block in spawn_thread' `
@ConfusedVorlon
Copy link
Author

I'm working around this with

ActsAsTenant.configure do |config|
  config.require_tenant = lambda do
    if $request_env.present?
      return false if $request_env["REQUEST_PATH"].start_with?("/rails/")
    end
    return true
  end
end

@Skulli
Copy link

Skulli commented Jan 10, 2024

i am also getting exceptions since the upgrade to rails 7.1 with this gem and representations. With 7.0 i had no problems

Here is a log/backtrace:

ActsAsTenant::Errors::NoTenantSet (ActsAsTenant::Errors::NoTenantSet):

acts_as_tenant (1.0.1) lib/acts_as_tenant/model_extensions.rb:21:in `block in acts_as_tenant'
activerecord (7.1.2) lib/active_record/scoping/default.rb:163:in `instance_exec'
activerecord (7.1.2) lib/active_record/scoping/default.rb:163:in `block (2 levels) in build_default_scope'
activerecord (7.1.2) lib/active_record/scoping/default.rb:159:in `each'
activerecord (7.1.2) lib/active_record/scoping/default.rb:159:in `inject'
activerecord (7.1.2) lib/active_record/scoping/default.rb:159:in `block in build_default_scope'
activerecord (7.1.2) lib/active_record/scoping/default.rb:197:in `evaluate_default_scope'
activerecord (7.1.2) lib/active_record/scoping/default.rb:158:in `build_default_scope'
activerecord (7.1.2) lib/active_record/scoping/named.rb:46:in `default_scoped'
activerecord (7.1.2) lib/active_record/scoping/named.rb:40:in `scope_for_association'
activerecord (7.1.2) lib/active_record/associations/preloader/association.rb:291:in `build_scope'
activerecord (7.1.2) lib/active_record/associations/preloader/association.rb:182:in `scope'
activerecord (7.1.2) lib/active_record/associations/preloader/association.rb:163:in `loader_query'
activerecord (7.1.2) lib/active_record/associations/preloader/batch.rb:41:in `each'
activerecord (7.1.2) lib/active_record/associations/preloader/batch.rb:41:in `group_by'
activerecord (7.1.2) lib/active_record/associations/preloader/batch.rb:41:in `group_and_load_similar'
activerecord (7.1.2) lib/active_record/associations/preloader/batch.rb:27:in `call'
activerecord (7.1.2) lib/active_record/associations/preloader.rb:121:in `call'
activerecord (7.1.2) lib/active_record/relation.rb:867:in `block in preload_associations'
activerecord (7.1.2) lib/active_record/relation.rb:866:in `each'
activerecord (7.1.2) lib/active_record/relation.rb:866:in `preload_associations'
activerecord (7.1.2) lib/active_record/relation.rb:951:in `block in exec_queries'
activerecord (7.1.2) lib/active_record/relation.rb:1003:in `skip_query_cache_if_necessary'
activerecord (7.1.2) lib/active_record/relation.rb:941:in `exec_queries'
activerecord (7.1.2) lib/active_record/association_relation.rb:44:in `exec_queries'
activerecord (7.1.2) lib/active_record/relation.rb:727:in `load'
activerecord (7.1.2) lib/active_record/relation.rb:264:in `records'
activerecord (7.1.2) lib/active_record/relation/delegation.rb:100:in `each'
activestorage (7.1.2) app/models/active_storage/blob.rb:380:in `touch_attachment_records'
activesupport (7.1.2) lib/active_support/callbacks.rb:403:in `block in make_lambda'
activesupport (7.1.2) lib/active_support/callbacks.rb:239:in `block in halting_and_conditional'
activesupport (7.1.2) lib/active_support/callbacks.rb:602:in `block in invoke_after'
activesupport (7.1.2) lib/active_support/callbacks.rb:602:in `each'
activesupport (7.1.2) lib/active_support/callbacks.rb:602:in `invoke_after'
activesupport (7.1.2) lib/active_support/callbacks.rb:111:in `run_callbacks'
activesupport (7.1.2) lib/active_support/callbacks.rb:952:in `_run_update_callbacks'
activerecord (7.1.2) lib/active_record/callbacks.rb:449:in `_update_record'
activerecord (7.1.2) lib/active_record/timestamp.rb:120:in `_update_record'
activerecord (7.1.2) lib/active_record/persistence.rb:1220:in `create_or_update'
activerecord (7.1.2) lib/active_record/callbacks.rb:441:in `block in create_or_update'
activesupport (7.1.2) lib/active_support/callbacks.rb:121:in `block in run_callbacks'
activerecord (7.1.2) lib/active_record/autosave_association.rb:375:in `around_save_collection_association'
activesupport (7.1.2) lib/active_support/callbacks.rb:130:in `block in run_callbacks'
activesupport (7.1.2) lib/active_support/callbacks.rb:141:in `run_callbacks'
activesupport (7.1.2) lib/active_support/callbacks.rb:952:in `_run_save_callbacks'
activerecord (7.1.2) lib/active_record/callbacks.rb:441:in `create_or_update'
activerecord (7.1.2) lib/active_record/timestamp.rb:125:in `create_or_update'
activerecord (7.1.2) lib/active_record/persistence.rb:718:in `save'
activerecord (7.1.2) lib/active_record/validations.rb:49:in `save'
activerecord (7.1.2) lib/active_record/transactions.rb:309:in `block in save'
activerecord (7.1.2) lib/active_record/transactions.rb:365:in `block in with_transaction_returning_status'
activerecord (7.1.2) lib/active_record/connection_adapters/abstract/transaction.rb:535:in `block in within_new_transaction'
activesupport (7.1.2) lib/active_support/concurrency/null_lock.rb:9:in `synchronize'
activerecord (7.1.2) lib/active_record/connection_adapters/abstract/transaction.rb:532:in `within_new_transaction'
activerecord (7.1.2) lib/active_record/connection_adapters/abstract/database_statements.rb:344:in `transaction'
activerecord (7.1.2) lib/active_record/transactions.rb:361:in `with_transaction_returning_status'
activerecord (7.1.2) lib/active_record/transactions.rb:309:in `save'
activerecord (7.1.2) lib/active_record/suppressor.rb:52:in `save'
activestorage (7.1.2) lib/active_storage/attached/one.rb:61:in `attach'
activestorage (7.1.2) app/models/active_storage/preview.rb:101:in `block (2 levels) in process'
activerecord (7.1.2) lib/active_record/connection_handling.rb:361:in `with_role_and_shard'
activerecord (7.1.2) lib/active_record/connection_handling.rb:147:in `connected_to'
activestorage (7.1.2) app/models/active_storage/preview.rb:100:in `block in process'
activestorage (7.1.2) lib/active_storage/previewer/mupdf_previewer.rb:26:in `block (2 levels) in preview'
activestorage (7.1.2) lib/active_storage/previewer.rb:55:in `block in draw'
activestorage (7.1.2) lib/active_storage/previewer.rb:63:in `open_tempfile'
activestorage (7.1.2) lib/active_storage/previewer.rb:50:in `draw'
activestorage (7.1.2) lib/active_storage/previewer/mupdf_previewer.rb:33:in `draw_first_page_from'
activestorage (7.1.2) lib/active_storage/previewer/mupdf_previewer.rb:25:in `block in preview'
activestorage (7.1.2) lib/active_storage/downloader.rb:15:in `block in open'
activestorage (7.1.2) lib/active_storage/downloader.rb:24:in `open_tempfile'
activestorage (7.1.2) lib/active_storage/downloader.rb:12:in `open'
activestorage (7.1.2) lib/active_storage/service.rb:92:in `open'
activestorage (7.1.2) app/models/active_storage/blob.rb:298:in `open'
activestorage (7.1.2) lib/active_storage/previewer.rb:32:in `download_blob_to_tempfile'
activestorage (7.1.2) lib/active_storage/previewer/mupdf_previewer.rb:24:in `preview'
activestorage (7.1.2) app/models/active_storage/preview.rb:99:in `process'
activestorage (7.1.2) app/models/active_storage/preview.rb:49:in `processed'
activestorage (7.1.2) app/controllers/active_storage/representations/base_controller.rb:14:in `set_representation'
activesupport (7.1.2) lib/active_support/callbacks.rb:403:in `block in make_lambda'
activesupport (7.1.2) lib/active_support/callbacks.rb:202:in `block (2 levels) in halting'
actionpack (7.1.2) lib/abstract_controller/callbacks.rb:34:in `block (2 levels) in <module:Callbacks>'
activesupport (7.1.2) lib/active_support/callbacks.rb:203:in `block in halting'
activesupport (7.1.2) lib/active_support/callbacks.rb:598:in `block in invoke_before'
activesupport (7.1.2) lib/active_support/callbacks.rb:598:in `each'
activesupport (7.1.2) lib/active_support/callbacks.rb:598:in `invoke_before'
activesupport (7.1.2) lib/active_support/callbacks.rb:119:in `block in run_callbacks'
activesupport (7.1.2) lib/active_support/callbacks.rb:130:in `block in run_callbacks'
actiontext (7.1.2) lib/action_text/rendering.rb:23:in `with_renderer'
actiontext (7.1.2) lib/action_text/engine.rb:69:in `block (4 levels) in <class:Engine>'
activesupport (7.1.2) lib/active_support/callbacks.rb:130:in `instance_exec'
activesupport (7.1.2) lib/active_support/callbacks.rb:130:in `block in run_callbacks'
activesupport (7.1.2) lib/active_support/callbacks.rb:141:in `run_callbacks'
actionpack (7.1.2) lib/abstract_controller/callbacks.rb:258:in `process_action'
actionpack (7.1.2) lib/action_controller/metal/rescue.rb:25:in `process_action'
actionpack (7.1.2) lib/action_controller/metal/instrumentation.rb:74:in `block in process_action'
activesupport (7.1.2) lib/active_support/notifications.rb:206:in `block in instrument'
activesupport (7.1.2) lib/active_support/notifications/instrumenter.rb:58:in `instrument'
activesupport (7.1.2) lib/active_support/notifications.rb:206:in `instrument'
actionpack (7.1.2) lib/action_controller/metal/instrumentation.rb:73:in `process_action'
actionpack (7.1.2) lib/action_controller/metal/params_wrapper.rb:261:in `process_action'
activerecord (7.1.2) lib/active_record/railties/controller_runtime.rb:32:in `process_action'
actionpack (7.1.2) lib/abstract_controller/base.rb:160:in `process'
actionview (7.1.2) lib/action_view/rendering.rb:40:in `process'
actionpack (7.1.2) lib/action_controller/metal.rb:227:in `dispatch'
actionpack (7.1.2) lib/action_controller/metal.rb:309:in `dispatch'
actionpack (7.1.2) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (7.1.2) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (7.1.2) lib/action_dispatch/journey/router.rb:51:in `block in serve'
actionpack (7.1.2) lib/action_dispatch/journey/router.rb:131:in `block in find_routes'
actionpack (7.1.2) lib/action_dispatch/journey/router.rb:124:in `each'
actionpack (7.1.2) lib/action_dispatch/journey/router.rb:124:in `find_routes'
actionpack (7.1.2) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (7.1.2) lib/action_dispatch/routing/route_set.rb:882:in `call'
vendor/middleware/account_middleware.rb:36:in `call'
warden (1.2.9) lib/warden/manager.rb:36:in `block in call'
warden (1.2.9) lib/warden/manager.rb:34:in `catch'
warden (1.2.9) lib/warden/manager.rb:34:in `call'
rack (2.2.8) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.2.8) lib/rack/etag.rb:27:in `call'
rack (2.2.8) lib/rack/conditional_get.rb:27:in `call'
rack (2.2.8) lib/rack/head.rb:12:in `call'
actionpack (7.1.2) lib/action_dispatch/http/permissions_policy.rb:36:in `call'
actionpack (7.1.2) lib/action_dispatch/http/content_security_policy.rb:33:in `call'
rack (2.2.8) lib/rack/session/abstract/id.rb:266:in `context'
rack (2.2.8) lib/rack/session/abstract/id.rb:260:in `call'
actionpack (7.1.2) lib/action_dispatch/middleware/cookies.rb:689:in `call'
actionpack (7.1.2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (7.1.2) lib/active_support/callbacks.rb:101:in `run_callbacks'
actionpack (7.1.2) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
actionpack (7.1.2) lib/action_dispatch/middleware/debug_exceptions.rb:29:in `call'
actionpack (7.1.2) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (7.1.2) lib/rails/rack/logger.rb:37:in `call_app'
railties (7.1.2) lib/ra...

@Skulli
Copy link

Skulli commented Jan 11, 2024

See config.active_storage.touch_attachment_records

With the setting config.active_storage.touch_attachment_records you can disable the touch-behaviour. But not sure if thats the initial cause of the issue. Since before that change the touch-behaviour was true as default.
See rails/rails@2cd8ac1

@ConfusedVorlon
Copy link
Author

it looks like that disable_touch option doesn't exist in 7.1.2

in 7.1.2, the code is simply:

def touch_attachment_records
  attachments.includes(:record).each do |attachment|
    attachment.touch
  end
end

@Skulli
Copy link

Skulli commented Jan 12, 2024

It should, just put

config.active_storage.touch_attachment_records = false inside application.rb

edit: you might be right, rails/rails#49723 looks like it is only in main so far.

@ConfusedVorlon
Copy link
Author

It's always fun when you have an issue in your app - and google leads you back to your own issue on github :)

just to round this out. My old fix stopped working

ActsAsTenant.configure do |config|
  config.require_tenant = lambda do
    if $request_env.present?
      return false if $request_env["REQUEST_PATH"].start_with?("/rails/")
    end
    return true
  end
end

as $request_env is no longer populated (in my app at least)

but

config.active_storage.touch_attachment_records = false inside application.rb

works a treat. Thanks @Skulli

@ConfusedVorlon
Copy link
Author

ConfusedVorlon commented Oct 1, 2024

I spoke too soon.

config.active_storage.touch_attachment_records = false inside application.rb

fixes the issue in dev - but not in production.
I don't know why yet...

@ConfusedVorlon
Copy link
Author

I have a new fix. I think this is pretty clean...

#app/controllers/concerns/disable_acts_as_tenant.rb

module DisableActsAsTenant
  extend ActiveSupport::Concern

  included do
    prepend_around_action :without_tenant
  end

  private

  def without_tenant
    ActsAsTenant.without_tenant do
      yield
    end
  end

end

Then in my acts_as_tenant.rb initializer

#config/initializers/acts_as_tenant.rb
require "acts_as_tenant/sidekiq" if defined? Sidekiq

ActsAsTenant.configure do |config|
  config.require_tenant = true
end

# Acts as tenant still triggering in production on representation generation
# https://github.com/ErwinM/acts_as_tenant/issues/330
Rails.application.config.after_initialize do
  if defined?(ActiveStorage::Representations::BaseController)
    ActiveStorage::Representations::BaseController.include(DisableActsAsTenant)
    Rails.logger.info("DisableActsAsTenant included in ActiveStorage::Representations::BaseController")
  else
    Rails.logger.warn("ActiveStorage::Representations::BaseController is not defined.")
  end
end


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants