Search code examples
ruby-on-railsruby-on-rails-5carrierwave

Carrierwave object.url VS object.image_url


In my Rails 5 app I am using Carrierwave to upload images.

I have to model that uses the same uploader:

account.rb:

mount_uploader :logo, ImageUploader

image.rb:

mount_uploader :image, ImageUploader

This uploads the file to:

"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"

The strange this now is that I can use:

@account.logo&.url(:thumb) // works!
@account.logo&.image_url(:thumb) // error!

But on the image model (one product has many images):

@product.images.first&.image_url(:thumb) // works!
@product.images.first&.url(:thumb) // error!

So in the first case I have to use .url and in the second one .image_url

An I have no idea why...any help?


Solution

  • The instance method image_url is defined dynamically based on the column that is passed to mount_uploader and simply calls url on the column. The definition looks like this...

    def #{column}_url(*args)
      #{column}.url(*args)
    end
    

    So, I would suspect that logo_url would work on @account (although I have not tested this)

    source