我使用paperclip来处理图像上传,资产类是多态的。我想在文件的路径中添加一个名为global_id的值,以防止所有内容被转储到单个目录中。具有资产的每个对象都将具有唯一的global_id,并且看起来回形针插值应该处理这个问题。
class Asset < ActiveRecord::Base
belongs_to :assetable, :polymorphic => true
Paperclip.interpolates :global_path do |attachment, style|
# ???
object.global_id
end
has_attached_file :asset,
:hash_secret => "jtsomething",
#:url => "/images/:global_path/:hash_:basename_:style.:extension",
:path => UPLOAD_PATH + "/:global_path/:hash_:basename_:style.:extension",
:styles => { :medium => "200x200>", :thumb => "75x75>"
}
如何访问此关系另一端的global_id?
THX
答案 0 :(得分:0)
您可以使用assetable
访问者从模型实例访问关系的另一面,如同所有其他关系一样:
Paperclip.interpolates :global_path do |attachment, style|
attachment.instance.assetable.global_id
end
假设您将全局ID存储在对象的global_id
字段中,或者其模型具有方法global_id