将 Rails 个活动模型与两个不同的模型相关联

Associate Rails active model with two different models

有没有办法在 Rails 活动模型上使用 Ruby 的继承?

我有两个模型要添加评论。如果我可以只使用一个可以与两个模型相关联的 Comment 模型,那就太好了。

调查Polymorphic Associations

class Comment < ApplicationRecord
  belongs_to :commentable, polymorphic: true
end

class Article < ApplicationRecord
  has_many :comments, as: :commentable
end

class Image < ApplicationRecord
  has_many :comments, as: :commentable
end