Remy Bartolotta
Remy Bartolotta

Reputation: 147

Why does carrierwave save a copy of the entire model?

I have been using CarrierWave with AWS forever without issue, however in my latest project I simply cannot get it work. The file uploads to AWS without any issue, however instead of saving the file location in the DB table, it store a copy of the entire model. I when the page loads, I get an error because rails cannot figure out what to do with the data stored in the table
Check out below.. directly from rails console where profile_picture is the field using mounted to CarrierWave.

It seeme like the entire object is being saved instead of a generated URL. Does anyone have any thoughts on this?

dog = Dog.first
=> #<Dog id: 2, user_id: nil, name: "Lentil", profile_picture: "971935_556519314400437_65392555_n.jpg", breed_id: 2, 
color: "fawn with black mask", age: 1, gender: "m", created_at: "2013-08-03 17:57:00", updated_at: "2013-08-03 18:12:33">

dog.profile_picture
=> #<ImageUploader:0x007f83388b70f0 @model=#<Dog id: 2, user_id: nil, name: "Lentil", profile_picture: "971935_556519314400437_65392555_n.jpg", breed_id: 2, color: "fawn with black mask", age: 1, gender: "m", created_at: "2013-08-03 17:57:00", updated_at: "2013-08-03 18:12:33">, @mounted_as=:profile_picture, @storage=#
<CarrierWave::Storage::Fog:0x007f83388b6b50 @uploader=#<ImageUploader:0x007f83388b70f0 ...>, @connection=#<Fog::Storage::AWS::Real:0x007f8336d5e3d0 @aws_access_key_id="xxx", @aws_secret_access_key="xxx", @hmac=#<Fog::HMAC:0x007f8336d5e100 @key="xxx", @digest=#<OpenSSL::Digest::Digest: xxx>, @signer=#<Proc:0x007f8336d5ddb8@/app/vendor/bundle/ruby/2.0.0/gems/fog-0.9.0/lib/fog/core/hmac.rb:22 
(lambda)>>, @endpoint=nil, @host="s3.amazonaws.com", @path="/", @port=443, @scheme="https", @connection=#<Fog::Connection:0x007f8336d5db10 @excon=#<Excon::Connection:0x007f8336d5d8b8 @connection={:headers=>{}, :host=>"s3.amazonaws.com", :mock=>nil, :path=>"/", :port=>"443", :query=>nil, :scheme=>"https"}, @socket_key="s3.amazonaws.com:443">, @persistent=true>>>, @file=#<CarrierWave::Storage::Fog::File:0x007f83388b6510 @uploader=#<ImageUploader:0x007f83388b70f0 
...>, @base=#<CarrierWave::Storage::Fog:0x007f83388b6b50 @uploader=#<ImageUploader:0x007f83388b70f0 ...>, @connection=#<Fog::Storage::AWS::Real:0x007f8336d5e3d0 @aws_access_key_id="xxx", @aws_secret_access_key="xxx", @hmac=#<Fog::HMAC:0x007f8336d5e100 @key="xxx", @digest=#<OpenSSL::Digest::Digest: xxx>, @signer=#<Proc:0x007f8336d5ddb8@/app/vendor/bundle/ruby/2.0.0/gems/fog-0.9.0/lib/fog/core/hmac.rb:22 
(lambda)>>, @endpoint=nil, @host="s3.amazonaws.com", @path="/", @port=443, @scheme="https", @connection=#<Fog::Connection:0x007f8336d5db10 @excon=#<Excon::Connection:0x007f8336d5d8b8 @connection={:headers=>{}, :host=>"s3.amazonaws.com", :mock=>nil, :path=>"/", :port=>"443", :query=>nil, :scheme=>"https"}, @socket_key="s3.amazonaws.com:443">, @persistent=true>>>, @path="uploads/dog/profile_picture/2/971935_556519314400437_65392555_n.jpg">, @versions={}>

Upvotes: 0

Views: 344

Answers (2)

Remy Bartolotta
Remy Bartolotta

Reputation: 147

There was a problem with the gem.. after a reinstall, everything worked as it should.

Upvotes: 1

Logan Serman
Logan Serman

Reputation: 29880

Carrierwave overrides the getter when it mounts, so when you call profile_picture, it will return an instance of the uploader instead of the file URL. In the actual database, only the file URL is stored.

There are many different methods you can call on an uploader. The most common is url. You should read the Carrierwave documentation on Github before using the gem to avoid further confusion.

Upvotes: 0

Related Questions