[英]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.