【发布时间】:2019-05-14 03:26:24
【问题描述】:
给定以下多态关系:
class Note < ApplicationRecord
belongs_to :noteable, polymorphic: true
has_one :garden, foreign_key: :id, primary_key: :noteable_id
end
class Garden < ApplicationRecord
has_many :notes, as: :noteable
end
我想验证 noteable_type 是 Garden 以防止不匹配。
什么是解决这个问题的好方法?
【问题讨论】:
-
我认为多态关联是默认的。
-
你能解释一下为什么这很有用吗?
Note并没有真正拥有一个花园,它可能属于也可能不属于Garden,但它肯定没有花园的所有权,所以我试图更好地理解这个问题。
标签: ruby-on-rails ruby-on-rails-5 polymorphic-associations