nkulig
nkulig

Reputation: 47

CarrierWave File Upload store_dir in parent association

I have an association setup between a Request model and a Document model. Each Request has_many Documents and each Document belongs_to a Request. Uploading files and storing them into a folder works great. Currently, my store_dir method looks like this:

def store_dir
  "uploads/Requests/#{model.id}"
end

However, I was wondering how I would save the files by request.id and then model.id:

def store_dir
  "uploads/#{request.id}/#{model.id}"
end

Is this possible? Hoping someone could point me in the right direction. I'm not sure how to pass the request id into the DocumentUploader.

If more information needs to be provided please let me know!

Upvotes: 2

Views: 750

Answers (1)

koffeinfrei
koffeinfrei

Reputation: 2065

As model is the ActiveRecord instance that mounts the uploader you should be able to use the ActiveRecord associations. Since you state that Document belongs_to a Request (and I assume that Document is the ActiveRecord that mounts the uploader) you can get the Request's id with model.request_id (or model.request.id).

def store_dir
  "uploads/#{model.request_id}/#{model.id}"
end

Upvotes: 3

Related Questions