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

Active Storage Issue #448

Open
MatthewKennedy opened this issue May 8, 2021 · 6 comments
Open

Active Storage Issue #448

MatthewKennedy opened this issue May 8, 2021 · 6 comments

Comments

@MatthewKennedy
Copy link
Contributor

MatthewKennedy commented May 8, 2021

I found a strange behaviour when using acts_as_nested_set and has_one_attached on the same model.

If acts_as_nested_set is placed above has_one_attached acts as the object is a newly created item, the active storage attachment is lost when saved.

But if you create your item and the go to attach the image using active storage it works fine.

If you place has_one_attached above acts_as_nested_set then the behaviour changes slightly, it stall fails.

  TRANSACTION (4.2ms)  COMMIT
[ActiveJob] Enqueued ActiveStorage::AnalyzeJob (Job ID: 9460cc6e-83cb-4c87-bc86-e2de446574e1) to Async(default) with arguments: #<GlobalID:0x00007fc9396e9588 @uri=#<URI::GID gid://navinew/ActiveStorage::Blob/20>>
Redirected to http://localhost:3000/admin/menus/1/menu_items/153/edit
Completed 302 Found in 75ms (ActiveRecord: 26.6ms | Allocations: 35954)

  ActiveStorage::Blob Load (0.1ms)  SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 20], ["LIMIT", 1]]
[ActiveJob] [ActiveStorage::AnalyzeJob] [9460cc6e-83cb-4c87-bc86-e2de446574e1] Performing ActiveStorage::AnalyzeJob (Job ID: 9460cc6e-83cb-4c87-bc86-e2de446574e1) from Async(default) enqueued at 2021-05-08T13:32:24Z with arguments: #<GlobalID:0x00007fc93968add0 @uri=#<URI::GID gid://navinew/ActiveStorage::Blob/20>>
[ActiveJob] [ActiveStorage::AnalyzeJob] [9460cc6e-83cb-4c87-bc86-e2de446574e1]   Disk Storage (0.5ms) Downloaded file from key: gq0s3ce5ug1v8t9q6e957lqurzha
[ActiveJob] [ActiveStorage::AnalyzeJob] [9460cc6e-83cb-4c87-bc86-e2de446574e1] Error performing ActiveStorage::AnalyzeJob (Job ID: 9460cc6e-83cb-4c87-bc86-e2de446574e1) from Async(default) in 1.48ms: ActiveStorage::FileNotFoundError (ActiveStorage::FileNotFoundError):
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activestorage-6.1.3.2/lib/active_storage/service/disk_service.rb:145:in `rescue in stream'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activestorage-6.1.3.2/lib/active_storage/service/disk_service.rb:138:in `stream'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activestorage-6.1.3.2/lib/active_storage/service/disk_service.rb:29:in `block in download'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/notifications.rb:203:in `block in instrument'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/notifications.rb:203:in `instrument'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activestorage-6.1.3.2/lib/active_storage/service.rb:155:in `instrument'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activestorage-6.1.3.2/lib/active_storage/service/disk_service.rb:28:in `download'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activestorage-6.1.3.2/lib/active_storage/downloader.rb:32:in `download'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activestorage-6.1.3.2/lib/active_storage/downloader.rb:13:in `block in open'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activestorage-6.1.3.2/lib/active_storage/downloader.rb:24:in `open_tempfile'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activestorage-6.1.3.2/lib/active_storage/downloader.rb:12:in `open'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activestorage-6.1.3.2/lib/active_storage/service.rb:90:in `open'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activestorage-6.1.3.2/app/models/active_storage/blob.rb:276:in `open'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activestorage-6.1.3.2/lib/active_storage/analyzer.rb:33:in `download_blob_to_tempfile'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activestorage-6.1.3.2/lib/active_storage/analyzer/image_analyzer.rb:32:in `read_image'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activestorage-6.1.3.2/lib/active_storage/analyzer/image_analyzer.rb:21:in `metadata'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activestorage-6.1.3.2/app/models/active_storage/blob/analyzable.rb:51:in `extract_metadata_via_analyzer'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activestorage-6.1.3.2/app/models/active_storage/blob/analyzable.rb:29:in `analyze'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activestorage-6.1.3.2/app/jobs/active_storage/analyze_job.rb:11:in `perform'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activejob-6.1.3.2/lib/active_job/execution.rb:48:in `block in perform_now'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/callbacks.rb:117:in `block in run_callbacks'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/i18n-1.8.10/lib/i18n.rb:314:in `with_locale'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activejob-6.1.3.2/lib/active_job/translation.rb:9:in `block (2 levels) in <module:Translation>'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/callbacks.rb:126:in `instance_exec'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/callbacks.rb:126:in `block in run_callbacks'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/core_ext/time/zones.rb:66:in `use_zone'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activejob-6.1.3.2/lib/active_job/timezones.rb:9:in `block (2 levels) in <module:Timezones>'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/callbacks.rb:126:in `instance_exec'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/callbacks.rb:126:in `block in run_callbacks'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activejob-6.1.3.2/lib/active_job/instrumentation.rb:21:in `block in instrument'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/notifications.rb:203:in `block in instrument'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/notifications.rb:203:in `instrument'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activejob-6.1.3.2/lib/active_job/instrumentation.rb:31:in `instrument'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activejob-6.1.3.2/lib/active_job/instrumentation.rb:14:in `block (2 levels) in <module:Instrumentation>'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/callbacks.rb:126:in `instance_exec'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/callbacks.rb:126:in `block in run_callbacks'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activejob-6.1.3.2/lib/active_job/logging.rb:22:in `block in tag_logger'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/tagged_logging.rb:99:in `block in tagged'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/tagged_logging.rb:37:in `tagged'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/tagged_logging.rb:99:in `tagged'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activejob-6.1.3.2/lib/active_job/logging.rb:22:in `tag_logger'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activejob-6.1.3.2/lib/active_job/logging.rb:15:in `block (2 levels) in <module:Logging>'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/callbacks.rb:126:in `instance_exec'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/callbacks.rb:126:in `block in run_callbacks'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/callbacks.rb:137:in `run_callbacks'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activejob-6.1.3.2/lib/active_job/execution.rb:47:in `perform_now'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activejob-6.1.3.2/lib/active_job/execution.rb:25:in `block in execute'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/callbacks.rb:117:in `block in run_callbacks'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activejob-6.1.3.2/lib/active_job/railtie.rb:47:in `block (4 levels) in <class:Railtie>'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/execution_wrapper.rb:88:in `wrap'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/reloader.rb:72:in `block in wrap'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/execution_wrapper.rb:88:in `wrap'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/reloader.rb:71:in `wrap'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activejob-6.1.3.2/lib/active_job/railtie.rb:46:in `block (3 levels) in <class:Railtie>'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/callbacks.rb:126:in `instance_exec'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/callbacks.rb:126:in `block in run_callbacks'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3.2/lib/active_support/callbacks.rb:137:in `run_callbacks'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activejob-6.1.3.2/lib/active_job/execution.rb:23:in `execute'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/activejob-6.1.3.2/lib/active_job/queue_adapters/async_adapter.rb:70:in `perform'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/concurrent-ruby-1.1.8/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:363:in `run_task'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/concurrent-ruby-1.1.8/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:352:in `block (3 levels) in create_worker'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/concurrent-ruby-1.1.8/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:335:in `loop'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/concurrent-ruby-1.1.8/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:335:in `block (2 levels) in create_worker'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/concurrent-ruby-1.1.8/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:334:in `catch'
/Users/matt/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/concurrent-ruby-1.1.8/lib/concurrent-ruby/concurrent/executor/ruby_thread_pool_executor.rb:334:in `block in create_worker'
Started GET "/admin/menus/1/menu_items/153/edit" for ::1 at 2021-05-08 14:32:24 +0100
Processing by Spree::Admin::MenuItemsController#edit as HTML
  Parameters: {"menu_id"=>"1", "id"=>"153"}
@stale
Copy link

stale bot commented Jun 19, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jun 19, 2021
@stale stale bot closed this as completed Jun 26, 2021
@botandrose
Copy link
Contributor

botandrose commented Feb 22, 2024

This happened to me today with Rails 7.1.3 and ANS 3.6.0. I have worked around the issue by switching the order in the model:

class Record < ActiveRecord::Base
  # broken
  acts_as_nested_set
  has_one_attached :attachment

  # works
  has_one_attached :attachment
  acts_as_nested_set
end

@botandrose
Copy link
Contributor

Digging deeper, it appears that the various calls to AR::Base#reload in awesome_nested_set/model.rb are to blame. Commenting them out resolves the issue. It looks like #reload resets some ActiveStorage hooks?

@botandrose
Copy link
Contributor

Turns out this is expected behavior??

# lib/active_storage/attached/model.rb
module ActiveStorage::Attached::Model
  def reload(*) # :nodoc:
    super.tap { @attachment_changes = nil }
  end
end

Also perhaps related: rails/rails#40630

Do we try to campaign to get this fixed in future Rails releases? Or work around it in ANS? Both?

At the very least, a note in the README.

@botandrose
Copy link
Contributor

botandrose commented Feb 24, 2024

I have successfully worked around this issue by undoing the attachment reset behavior in ActiveStorage:

class Record < ActiveRecord::Base
  has_one_attached :attachment # must come before acts_as_nested_set
  acts_as_nested_set

  # undo ActiveStorage's reset behavior
  def reload(*)
    before = @attachment_changes
    super.tap { @attachment_changes = before }
  end
end

So the question is, where do we go with this to resolve the apparent incompatibility between AS and ANS? Should we incorporate these workarounds into ANS?

@botandrose
Copy link
Contributor

@parndt Would you mind reopening this issue? I plan to figure this out next.

@parndt parndt reopened this Mar 8, 2024
@stale stale bot removed the stale label Mar 8, 2024
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

3 participants