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

Error occurs when selecting columns with select and saving #2608

Closed
wonda-tea-coffee opened this issue Jun 3, 2022 · 0 comments · Fixed by #2613
Closed

Error occurs when selecting columns with select and saving #2608

wonda-tea-coffee opened this issue Jun 3, 2022 · 0 comments · Fixed by #2613

Comments

@wonda-tea-coffee
Copy link
Contributor

wonda-tea-coffee commented Jun 3, 2022

Abstract

When saving against the model retrieved by select, an error occurs and the image is updated to nil.

Step to reproduce

code

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org/"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "activerecord"
  gem "sqlite3"
  gem "carrierwave"
  gem "rmagick"
end

require "active_record"
require "minitest/autorun"
require "logger"
require 'carrierwave/orm/activerecord'

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :users, force: true do |t|
    t.string :avatar
  end
end

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick
end

class User < ActiveRecord::Base
  mount_uploader :avatar, ImageUploader
end

class BugTest < Minitest::Test
  def test_save_without_select
    User.create!
    assert User.last.save # test is pass
  end

  def test_save_with_select
    User.create!
    assert User.select(:id).last.save # error occurs!
  end
end

output

Finished in 0.010423s, 191.8821 runs/s, 95.9411 assertions/s.

  1) Error:
BugTest#test_save_with_select:
NoMethodError: undefined method `remove_previously_stored_files_after_update' for #<Object:0x00007f2757986d88>

        uploader.remove! if uploader.remove_previously_stored_files_after_update && !after_paths.include?(uploader.path)
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    /usr/local/bundle/gems/carrierwave-2.2.2/lib/carrierwave/mounter.rb:157:in `block in remove_previous'
    /usr/local/bundle/gems/carrierwave-2.2.2/lib/carrierwave/mounter.rb:156:in `each'
    /usr/local/bundle/gems/carrierwave-2.2.2/lib/carrierwave/mounter.rb:156:in `remove_previous'
    /usr/local/bundle/gems/carrierwave-2.2.2/lib/carrierwave/mount.rb:204:in `remove_previously_stored_profile_image_filename'
    /usr/local/bundle/gems/activesupport-7.0.3/lib/active_support/callbacks.rb:400:in `block in make_lambda'
    /usr/local/bundle/gems/activesupport-7.0.3/lib/active_support/callbacks.rb:261:in `block in conditional'
    /usr/local/bundle/gems/activesupport-7.0.3/lib/active_support/callbacks.rb:599:in `block in invoke_after'
    /usr/local/bundle/gems/activesupport-7.0.3/lib/active_support/callbacks.rb:599:in `each'
    /usr/local/bundle/gems/activesupport-7.0.3/lib/active_support/callbacks.rb:599:in `invoke_after'
    /usr/local/bundle/gems/activesupport-7.0.3/lib/active_support/callbacks.rb:108:in `run_callbacks'
    /usr/local/bundle/gems/activesupport-7.0.3/lib/active_support/callbacks.rb:929:in `_run_commit_callbacks'
    /usr/local/bundle/gems/activerecord-7.0.3/lib/active_record/transactions.rb:321:in `committed!'
    /usr/local/bundle/gems/activerecord-7.0.3/lib/active_record/connection_adapters/abstract/transaction.rb:155:in `commit_records'
    /usr/local/bundle/gems/activerecord-7.0.3/lib/active_record/connection_adapters/abstract/transaction.rb:304:in `block in commit_transaction'
    /usr/local/bundle/gems/activesupport-7.0.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
    /usr/local/bundle/gems/activesupport-7.0.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
    /usr/local/bundle/gems/activesupport-7.0.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
    /usr/local/bundle/gems/activesupport-7.0.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
    /usr/local/bundle/gems/activerecord-7.0.3/lib/active_record/connection_adapters/abstract/transaction.rb:294:in `commit_transaction'
    /usr/local/bundle/gems/activerecord-7.0.3/lib/active_record/connection_adapters/abstract/transaction.rb:345:in `block in within_new_transaction'
    /usr/local/bundle/gems/activesupport-7.0.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
    /usr/local/bundle/gems/activesupport-7.0.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
    /usr/local/bundle/gems/activesupport-7.0.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
    /usr/local/bundle/gems/activesupport-7.0.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
    /usr/local/bundle/gems/activerecord-7.0.3/lib/active_record/connection_adapters/abstract/transaction.rb:317:in `within_new_transaction'
    /usr/local/bundle/gems/activerecord-7.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:316:in `transaction'
    /usr/local/bundle/gems/activerecord-7.0.3/lib/active_record/transactions.rb:350:in `with_transaction_returning_status'
    /usr/local/bundle/gems/activerecord-7.0.3/lib/active_record/transactions.rb:298:in `save'
    /usr/local/bundle/gems/activerecord-7.0.3/lib/active_record/suppressor.rb:50:in `save'
    test.rb:48:in `test_save_with_select'

2 runs, 1 assertions, 0 failures, 1 errors, 0 skips

Question

I would like the test test_save_with_select to go through, is this the intended behavior?

For reference, this behavior was caused by the following commit.
67800fd

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

Successfully merging a pull request may close this issue.

1 participant