简体   繁体   English

Rails 4多个多态has_one关系不起作用

[英]Rails 4 multiple polymorphic has_one relations not working

I'm trying to get three polymorphic associations working from my Booth model with below 我试图从我的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

This however does not work and I'm getting same values in the view when I use build method on the resource for that association separately. 但是,这不起作用,当我在该关联的资源上分别使用build方法时,我在视图中获得了相同的值。

In my uploaded_files table in the database I have imageable_id and imageable_type I want to reuse this for things like company logo and booth main image because otherwise I have to create more columns to support other polymorphic associations and they will be left with null if not used. 在我uploaded_files在我已经在数据库表imageable_idimageable_type我想,否则我要创建多个列,以支持其他多态关联重用这对于像公司的标志和展位主图像,他们将留下null ,如果不使用。

Below is my model for UploadedFile : 下面是我的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

Is there a way to utilise one set of polymorphic columns for all of the above associations? 有没有一种方法可以将一组多态列用于上述所有关联?

The reason you are having this problem is because has_one will ensure that only one record in the target table ( uploaded_files in your case), will point back to the Booth model. 你有这个问题的原因是因为has_one将确保只有一个目标表(记录uploaded_files你的情况),将指向回到展台模型。 If you want to differentiate between different types of the same model, you'll need to set a flag somewhere that indicates which type it is. 如果要区分同一模型的不同类型,则需要在某处设置一个标志以指示其类型。

To do this, first you'll need to add a column that identifies the image type: 为此,首先您需要添加一列来标识图像类型:

# in a migration
def change
  add_column :uploaded_files, :image_type, :string
end

You can define the expected image types as constants: 您可以将期望的图像类型定义为常量:

class UploadedFile < ActiveRecord::Base
  IMAGE_TYPE_STANDARD = 'Standard'
  IMAGE_TYPE_LOGO     = 'Logo'
  IMAGE_TYPE_BOOTH    = 'Booth'
end

Now your associations should reflect these types: 现在,您的关联应反映以下类型:

class Booth < ActiveRecord::Base
  # optional - if you want to access all attached uploads
  has_many :uploaded_files, :as => :imageable, :dependent => :destroy

  has_one  :uploaded_file, :as => :imageable, :dependent => :destroy, 
           conditions: { image_type: UploadedFile::IMAGE_TYPE_STANDARD }
  has_one  :company_logo, :as => :imageable, :class_name => 'UploadedFile', 
           :dependent => :destroy, 
           conditions: { image_type: UploadedFile::IMAGE_TYPE_LOGO }
  has_one  :booth_main_image, :as => :imageable, :class_name => 'UploadedFile', 
           :dependent => :destroy, 
           conditions: { image_type: UploadedFile::IMAGE_TYPE_BOOTH }

  accepts_nested_attributes_for ...
end

Note that you won't have to set the image_type manually. 请注意,您image_type手动设置image_type Since the type is set by the condition, ActiveRecord will set it for you when you build or create off one of the has_one associations: 由于类型是由条件设置的,因此在您创建或创建has_one关联之一时,ActiveRecord会为您设置它:

upload = @booth.build_uploaded_file(asset: some_asset)
upload.image_type 
=> 'Standard'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM