Paperclip custom Interpolation in url

Folks,

I have a simple model hierarchy (reduced to example as below)

class Post < ActiveRecord::Base
has_many :comments
end

class Comment < ActiveRecord::Base
belongs_to :post
has_attached_file :photo,
# :url => “/assets/class_cal//:id/:style/:basename.:extension”
:url => “/assets/post/:post_date/:id/:style/:basename.:extension”
end

What I want to do: I am trying to insert the “original post date” in
the url with the :post_date (stored in the Post model) interpolation
function as defined in the Paperclip Interpolation module below.


module Paperclip
module Interpolations
def post_date attachment, style_name
attachment.<access the post class/model
here>.instance_read(:post_date).to_s
end
end
end

Problem: The problem I am running into is that I can’t seem to access
the post model object from the definition of post_date function above

  • the attachment I understand refers to the Comment object. I have
    tried attachment.post.instance_read(:post_date) but that doesn’t work.

Looking for thoughts/help on how to access a field in another model in
this situation?

Thanks for any pointers - I am on 2.3.8.

-S

On Jan 8, 5:32am, skt [email protected] wrote:

Problem: The problem I am running into is that I can’t seem to access
the post model object from the definition of post_date function above

  • the attachment I understand refers to the Comment object. I have
    tried attachment.post.instance_read(:post_date) but that doesn’t work.

isn’t attachment.instance what gives you the associated AR object (ie
attachment.instance.post would be what you are looking for here) ?

Fred