0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.