简体   繁体   English

1 个模型上的 rails 多态关联

[英]rails polymorphic association on 1 model

Does rails provide multiple polymorphic association. rails 是否提供多个多态关联。 And allow has_many relation.并允许 has_many 关系。

class Picture < ActiveRecord::Base
  belongs_to :image, polymorphic: true
  belongs_to :icon, polymorphic: true 
end

class Event < ActiveRecord::Base
  has_many :pictures, as: :image
  has_many :pictures, as: :icon
end

Thank you谢谢

Rails' active record does allow you to define a polymorphic association. Rails 的活动记录确实允许您定义多态关联。

From the docs :文档

class Picture < ApplicationRecord
  belongs_to :imageable, polymorphic: true
end

class Employee < ApplicationRecord
  has_many :pictures, as: :imageable
end

class Product < ApplicationRecord
  has_many :pictures, as: :imageable
end

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

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