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

#{column}_url equals to nil after saving object #2253

Closed
virolea opened this issue Nov 23, 2017 · 9 comments
Closed

#{column}_url equals to nil after saving object #2253

virolea opened this issue Nov 23, 2017 · 9 comments
Labels

Comments

@virolea
Copy link

virolea commented Nov 23, 2017

I came across an issue trying to save an image. I'm submitting a form with an image, and update an existing record with it. I'm doing through an xhr request from the client, and I want the request to send the image url back.

def update
  record = Record.find(params[:id])
  record.update(photo: params[:photo])
  render json: record.photo.url, status: :ok
end

Carriewave's doc specifies:

u = User.new
u.avatar = params[:file]

u.save!
u.avatar.url # => '/url/to/file.png'

Will help me get my file url. However my action returns nil but it works if I instantiate a new version of the record:

I did some testing in the console to show my point:
capture d ecran 2017-11-23 a 15 06 45

So the only way I can get the url of the image without having to fetch the record again, after having a look at the uploader object is through:

c.photo.metadata['url']
# => returns the url

Config:
Rails 5.1.4
Carrierwave 1.2.1

@gjamerson
Copy link

I'm having the same issue.
Rails 4.2.10
Carrierwave 1.2.1

@rbclark
Copy link

rbclark commented Mar 22, 2018

I believe I am experiencing this exact same issue and it seems to be exhibiting itself inside of my test suite. I hand off to a background worker where the ProductTest is updated and then have been running the following commands to compare the response.

pt.reload
=> nil
pt == ProductTest.last
=> true
pt.patient_archive.file
=> nil
ProductTest.last.patient_archive.file
=> #<CarrierWave::SanitizedFile:0x00007fd07bc67a20
 @content_type=nil,
 @file="/path/to/rails/public/data/upload/product_test/5ab4243dc60ac1cce7e23546/product_test-5ab4243dc60ac1cce7e23546.zip20180322-52455-1dmh03x",
 @original_filename=nil>
pt.patient_archive.file
=> nil

It seems that the reload is not properly refreshing the file, even though it did previously on Carrierwave (0.11.2) and carrierwave-mongoid (0.9.0)

I was originally going to post this on carrierwave-mongoid however it seems this issue already closely resembles my issues so I'm unsure if the issue is here or carrierwave-mongoid.

This is now happening for me one
rails (4.2.10)
carrierwave (1.2.2)
carrierwave-mongoid (1.0.0)

@rbclark
Copy link

rbclark commented Apr 3, 2018

Just wanted to bump this and see if there is any other info I can provide to help get this issue fixed? It is blocking a few other tasks I have so if there is anything I can do or provide please let me know.

@danderozier
Copy link

I seem to be having the same issue in Rails 5.2 and Carrierwave 1.2.2. The object returned after saving returns nil for the url, but instantiating a new object for the same record works as expected.

@virolea
Copy link
Author

virolea commented May 3, 2018

I might have a look at the code and submit a PR if I find the time.
Now that rails 5.2 and active storage are out, I might move from carrierwave in the near future

@p8
Copy link
Contributor

p8 commented Jun 28, 2018

I think the problem is in the following code:

uploader.retrieve_from_store!(identifier) if identifier.present?

  def uploaders
      @uploaders ||= read_identifiers.map do |identifier|
        uploader = blank_uploader
        uploader.retrieve_from_store!(identifier) if identifier.present?
        uploader
      end
  end

When a model is initialized before the identifier is set retrieve_from_store! will not be called because @uploaders is already set.

This did work differently in 0.11 (with single uploaders). retrieve_from_store! can be called later on when the identifier is set.

  def uploader
    @uploader ||= record.class.uploaders[column].new(record, column)
    @uploader.retrieve_from_store!(identifier) if @uploader.blank? && identifier.present?
    @uploader
  end

So a fix will probably be to move the call to retrieve_from_store! outside the loop.

@p8
Copy link
Contributor

p8 commented Jun 28, 2018

Unsetting the @uploaders instance variable on the mounter seems to fix it.

    attachment.url # => nil    
    _mounter(:attachment).instance_variable_set(:@uploaders, nil)
    attachment.url # => the correct url

@mshibuya
Copy link
Member

I don't understand what makes the attribute and @uploaders out of sync.
Can anyone reproduce this using a vanilla Rails app?

@mshibuya
Copy link
Member

Stale, please reopen on update.

@mshibuya mshibuya closed this as not planned Won't fix, can't repro, duplicate, stale Dec 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants