【发布时间】:2014-01-07 07:22:32
【问题描述】:
我正在尝试从我的Booth 模型中使用以下三个多态关联
has_one :uploaded_file, as: :imageable, dependent: :destroy
accepts_nested_attributes_for :uploaded_file
has_one :company_logo, as: :imageable, class_name: 'UploadedFile', dependent: :destroy
accepts_nested_attributes_for :company_logo
has_one :booth_main_image, as: :imageable, class_name: 'UploadedFile', dependent: :destroy
accepts_nested_attributes_for :booth_main_image
但是,这不起作用,当我在该关联的资源上单独使用 build 方法时,我在视图中得到相同的值。
在我的uploaded_files 数据库表中,我有imageable_id 和imageable_type
我想将此用于公司徽标和展位主图像之类的东西,因为否则我必须创建更多列来支持其他多态关联,如果不使用它们将留下null。
下面是UploadedFile的模型:
class UploadedFile < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
has_attached_file :assets
validates_attachment_content_type :assets,
:content_type => /^image\/(png|gif|jpeg)/,
:default_url => "/images/:style/missing.png",
:message => "only (png|gif|jpeg) images are allowed and the size cannot exceed 5mb",
:size => { :in => 0..5000.kilobytes }
end
有没有办法为上述所有关联使用一组多态列?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 paperclip polymorphic-associations