Awea
Awea

Reputation: 3173

Paperclip custom path with id belongs_to

I'm using paperclip inside a project.

The concerned model look like this :

class Asset < ActiveRecord::Base

  has_paper_trail                              # Track model with paper_trail
  has_attached_file :image, :styles => { :medium => "300x300>"}, 
  :path => ":rails_root/public/attachments/project_#{:project_id}/ressources/:basename_:style.:extension"

  belongs_to :project

end

As you see I want to have the id of concerned project in my image path, how can I make this ?

Upvotes: 3

Views: 1475

Answers (1)

DeathHammer
DeathHammer

Reputation: 660

Try this

has_attached_file :image, :styles => { :medium => "300x300>"}, 
 path => ":rails_root/public/attachments/:project_id/ressources/:basename_:style.:extension"

and then write

Paperclip.interpolates :project_id do |attachment, style|
    "project_#{attachment.instance.project_id}"
  end

Upvotes: 6

Related Questions