delvison
delvison

Reputation: 149

Rails Paperclip custom filepath

Ok so I have this model...

class Asset < ActiveRecord::Base
  attr_accessible :asset_file_name, :lesson_id, :attachment

  has_attached_file :attachment,
    :url => "/attachments/:id/:basename.:extension",
    :path => ":rails_root/public/attachments/:id/:basename.:extension"

  validates_presence_of :asset_file_name
  validates_attachment_presence :attachment
end

(also have a model for Lesson)

I want to save attachments to /attachments/:lesson_id/:basename.:extension. That is not to the :id of the asset but the :lesson_id. When I do this the directory is actually just named :lesson_id. Anyone know how to get the actual lesson_id? I've also tried @asset.lesson_id.

Upvotes: 0

Views: 376

Answers (1)

Draiken
Draiken

Reputation: 3815

You have to add a custom interpolator in paperclip. This is best done in an initializer or somewhere decoupled from the model

Paperclip.interpolates('lesson_id') do |attachment, style|
  attachment.instance.lesson_id
end

After that your :lesson_id will be the actual object.lesson_id

Upvotes: 1

Related Questions