0

我的情况很糟糕,单表继承的实现很糟糕。我想知道的是“后代”类是否可以破坏其“父类”的关联。我不认为是这种情况,但我想确认我是否可以。

以下是有问题的简化模型:

class CertificateName < ApplicationRecord
  has_one :ssl_account, through: :certificate_content
end

class Domain < CertificateName
  has_one :ssl_account, touch: true
end 

这是简化的架构):

create_table "certificate_names", force: :cascade do |t|
    t.integer  "certificate_content_id" 
    t.integer  "ssl_account_id"
  end

在上面的场景中,继承的Domain类是直接关联ssl_account还是通过CertificateName模型关联?根据我所看到的,我认为在我的情况下ssl_account与模型有直接关系并且不遵循has_one :through想法?

4

1 回答 1

0

是否可以通过单个表继承来破坏 Rails 关联?

是的。

当您声明具有相同名称的多个关联时,最后评估的关联将始终破坏现有关联,无论它在哪里声明。

关联宏(belongs_to, has_one, has_many, has_and_belongs_to_many)既生成方法,也生成关联反射,它简单地保存在类似哈希的结构中,因此传递相同的键(关联的名称)只会破坏它。

class Domain < CertificateName
  has_one :ssl_account, touch: true
end 

将始终直接加入ssl_accounts并期望它具有certificate_name_id外键列。

于 2020-06-11T09:04:08.187 回答