是否可以将选项添加到在超类中声明的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'