Filename override problem while accessing model id

856 views
Skip to first unread message

Nic

unread,
Mar 30, 2011, 2:33:30 PM3/30/11
to carrierwave
I am essentially having problem 1 outlined here
http://groups.google.com/group/carrierwave/browse_thread/thread/e364148327183edf/04df2a13a5b174bb?lnk=gst&q=override+filename#04df2a13a5b174bb

This following code sets the actual content filename correctly, but
the path to the content is incorrect and is missing the model id in
the filename portion. I've also tried model.id.to_s

# Override the filename of the uploaded files:
def filename
"#{model.id}#{File.extname(original_filename.to_s)}" if
original_filename
end

So if my model id is 3 and the original filename is audiofile.wav, the
actual file in the file system gets correctly set to 3.wav, but the
path to the file is incorrectly set to assets/.wav and is therefore
missing the model.id portion.

Thanks for any help,
-Nic

Trevor Turk

unread,
Mar 30, 2011, 2:49:03 PM3/30/11
to carri...@googlegroups.com
On Wednesday, March 30, 2011 1:33:30 PM UTC-5, Nic wrote:
  # Override the filename of the uploaded files:
  def filename
    "#{model.id}#{File.extname(original_filename.to_s)}" if
original_filename
  end

Don't use model.id in filename. I committed a small change a few days ago that should help other people with this, too:


- Trevor

Nic

unread,
Mar 30, 2011, 3:06:04 PM3/30/11
to carrierwave
So there's no workaround to this problem then? You simply can't
include any sort of dynamic naming scheme in your filenames?
> https://github.com/jnicklas/carrierwave/commit/951bf30e112ab0789f7afc...
>
> - Trevor

Jonas Nicklas

unread,
Mar 31, 2011, 2:31:47 AM3/31/11
to carri...@googlegroups.com, Nic
You can use anything you want, as long as it's not the id of the
model. CarrierWave puts the name of the file into the database, you
can't use the model id, because CarrierWave would have to know it
before putting the record into the DB. Makes sense?

/Jonas

> --
> You received this message because you are subscribed to the Google Groups "carrierwave" group.
> To post to this group, send email to carri...@googlegroups.com.
> To unsubscribe from this group, send email to carrierwave...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/carrierwave?hl=en.
>
>

dblock

unread,
Mar 31, 2011, 8:26:42 AM3/31/11
to carrierwave
This might help: http://code.dblock.org/ShowPost.aspx?id=187

In the uploader you can do about whatever you want.

def store_path(for_file = filename)
self.model.class.name.underscore.pluralize + "/" + self.model.slug +
"/" + (version_name || :original).to_s + ".jpg"
end

Nic

unread,
Mar 31, 2011, 10:35:55 AM3/31/11
to carrierwave
Thanks dblock, but if I do that the paths in carrierwave aren't quite
right and I can't retrieve the file.

I guess that makes sense Jonas. I think I will end up writing an
after_save method that updates the filename if it isn't already equal
to the model id.

Magne Matre Gåsland

unread,
Jan 26, 2015, 9:28:42 AM1/26/15
to carri...@googlegroups.com, nicolas...@gmail.com
Hi Jonas,

I am having to deal with some code where they've used model.id in the store_dir path
for the images they stored. I have a backup of the images, but I don't have the
model.id's for them.

I have recreated a backup of the database they used, so it might be in there somewhere, 
although currently I'm getting broken image url's for all the images.

Where can I find the model.id's that carrierwave uses?

Where is the documentation for model.id ?

- Magne
Reply all
Reply to author
Forward
0 new messages