簡體   English   中英

1 個模型上的 rails 多態關聯

[英]rails polymorphic association on 1 model

rails 是否提供多個多態關聯。 並允許 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

謝謝

Rails 的活動記錄確實允許您定義多態關聯。

文檔

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