HowTo? Set url without fetching file

1,630 views
Skip to first unread message

Marc

unread,
Apr 25, 2012, 3:28:44 PM4/25/12
to carrierwave
I would like to manually set the location of an asset, ie.
http://example.com/example.png, and save the model, without
carrierwave downloading the image.

The reason is that the image is already stored in my own S3 bucket,
and I'm already using the same S3 bucket with my carrierwave upload
and fog, so all it is doing is downloading it and then uploading it
again. Also, I'm not performing any post-upload processing of the
file, so downloading really is unnecessary. So, I only have 1 version
of the file, and it's already in a location where carrierwave/fog can
fetch it. I would, however, like to continue to use carrierwave/fog
because it offers nice getter methods for my assets from S3.

I am using the remote_path_url method to set the url, but I know that
carrierwave fetches the image before uploading it/storing it. Of
course it makes sense that carrierwave should do that 99% of the time,
but this is the 1%.

I have tried overwriting the setter method in the model, but that
didn't seem to persist for some reason. Is there a way to easily do
this with carrierwave that you can think of?

Marc

unread,
Apr 26, 2012, 10:20:42 AM4/26/12
to carrierwave
Solution #1 - Update the column manually through SQL command.

ActiveRecord::Base.connection.execute("UPDATE `<<CLASS>>` SET
`<<CARRIER WAVE COLUMN>>`=#{ActiveRecord::Base.sanitize(url)} WHERE
`<<CLASS>>`.`id` = #{<<CLASS>>.id}")



On Apr 25, 3:28 pm, Marc <m...@getteamup.com> wrote:
> I would like to manually set the location of an asset, ie.http://example.com/example.png, and save the model, without

Marc

unread,
Apr 26, 2012, 11:38:59 AM4/26/12
to carrierwave
Solution #2 - much better. Seems to work like a charm too. :)

@<<CLASS INSTANCE>> = <<CLASS>>.new({foo:'bar'})
@<<CLASS INSTANCE>>.raw_write_attribute(:<<CARRIERWAVE COLUMN>>, url)

Matenia Rossides

unread,
May 11, 2012, 10:14:12 PM5/11/12
to carri...@googlegroups.com
This will work in rails 3.x using @<<class instance>>.write_attribute as well .. but not for rails 2.3.x

I recently had to do this in an older app, and ended up having to do some nested looping with:

Model.update_all(:id => existing_id, :image => 'custom_file_name.png')

This seemed to work. But write_attribute or raw_write_attribute (rails > 3.1) is much nicer
Reply all
Reply to author
Forward
0 new messages