【发布时间】:2013-08-20 10:21:12
【问题描述】:
我有 3 个模型应该有相关的图像:
Program.rb
Episode.rb
Gallery.rb
其中任何一个都需要具有不同的图像尺寸。例如:
Program -> 100x100 , 250x400 , 500x400
Episode -> 222x450 , 210x330 , 1000x1200
Gallery -> 100x100 , 500x400 , 1000x1200
首先,我认为Picture 模型has_attached_file(Paperclip 的助手)的polymorphic 关联就足够了。就像这样:
class Picture < ActiveRecord::Base
has_many :imageable, :polypmorphic=> :true
has_attached_file :image, :styles => {... :program1 => "100x100>", :program2 => "250x400>", :episode1=>"222x450" , :episode2=>"210x330" , :gallery1=>"100x100" , :gallery2=>"500x400" .... }
# There are almost 10 different styles for different models...
end
通过这种方式,我可以像这样使用图像:
@program.pictures.first.image[:program1]
@episode.pictures.second.image[:episode2]
..
.
但我不认为它是有效的?是吗?
处理这种需求的最佳策略是什么?
我应该如何建立我的联想和回形针?
例如,为每个模型设置回形针更好吗?
我认为在一张桌子上收集所有图像是一种好习惯?
你怎么看?
这实际上应该是一个常见的问题?不是吗?
谢谢
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 paperclip ruby-on-rails-4