关于 belong_to、has_one、has_many 的困惑

Confusion regarding belong_to, has_one, has_many

我想在我的项目中扩展 Shift 模型的功能。 我有两种类型的班次 Ashift 和 Bshift,它们与 Shift 直接相关。

我的困惑源于如何在 Shift 中设置关联。这是我的:

我想说的是 Shift 有一个 或另一个 依赖 shift class,但不是两者都有,也不是两者都没有。

class Ashift < ApplicationRecord
  belongs_to :shift
end
class Bshift < ApplicationRecord
  belongs_to :shift
end

class Shift < ApplicationRecord
  has_one :ashift
  has_one :bshift
end

我相信 association guide 中已经清楚地列出了这一点,但我已经读了好几遍了,但我仍然有点困惑。

has_one 或 has_many 是指模型的每个实例,还是整个模型 class?有没有不同的或更好的方法来做到这一点?

use 有一个 STI readmore about this here

class TheShift < ApplicationRecord
  belongs_to :shift
end

class Ashift < TheShift 
end

class Bshift < TheShift 
end

class Shift < ApplicationRecord
  has_one :theshift
end