在Rails的子类中为has_one关联添加选项



是否可以将选项添加到在超类中声明的has_one关联中,而不会丢失在超类内声明的选项?类似这样的东西:

class AbstractBar < ActiveRecord::Base
  self.abstract_class = true
  has_one :foo, dependent: :destroy
  // Shared validations and stuff for the :foo association
end
class BarA < AbstractBar
  // BarA should have association to FooA
  has_one :foo, class_name: foo_a
end
class BarB < AbstractBar
  // BarB should have association to FooB
  has_one :foo, class_name: foo_b
end

以上示例将不起作用,因为dependent: :destroy将在子类中丢失。

是的,这是可能的。但在BarA中foo的foreign_key是bar_a_id,在BarB中的foo的foreign_key则是bar_b_id

CCD_ 6和CCD_ 7可能是CCD_
所以你应该添加像这样的外键选项

has_one :foo, class_name: 'FooA', foreign_key: 'bar_id'

最新更新