回形针和多态关联

时间:2014-02-10 09:50:48

标签: ruby ruby-on-rails-3 paperclip polymorphic-associations

我有多个需要与之关联的图像的模型,因此设置了多态关联。

目前唯一的事情就是如果我为“Post”创建一个图像并希望它是“200x200”,这意味着如果我在我的图像模型中声明了多个样式,那么我将获得多个尺寸我不一定需要的图像。

有没有办法声明“Post”模型应该只保存一个或两个样式,即使声明了5个维度。

以下示例

class Image < ActiveRecord::Base
belongs_to :imageable, polymorphic: true

attr_accessible :photo
has_attached_file :photo, :styles => { :small_blog => "250x250#", :large_blog => "680x224#", :thumb => "95x95#", :portfolio_square => "300x300#", :portfolio_small => "220x220#", :portfolio_large => "680x680#" },
:storage => :s3,
:url  => ":s3_domain_url",
:s3_protocol => 'http',
:path => "/images/:id/:style.:extension",
:s3_credentials => {
 :bucket => ENV['AWS_BUCKET'],
 :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
 :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
 end


class Post < ActiveRecord::Base

has_many :images, as: :imageable, :dependent => :destroy
accepts_nested_attributes_for :images
attr_accessible :image_id, :images_attributes, :imageable_id, :imageable_attributes


end

我如何只保存特定样式?不是所有人......有没有人这样做过?

由于

0 个答案:

没有答案
相关问题