Ruby on rails Rails多态关联覆盖了具有急切加载的getter
我有一些设计很糟糕的东西。它与重写的getter有某种多态关联,例如Ruby on rails Rails多态关联覆盖了具有急切加载的getter,ruby-on-rails,ruby,ruby-on-rails-3,polymorphic-associations,Ruby On Rails,Ruby,Ruby On Rails 3,Polymorphic Associations,我有一些设计很糟糕的东西。它与重写的getter有某种多态关联,例如 class Whtvr belongs_to :assoc, polymorphic: true def assoc if assoc_type == 'a_type' AType.find(assoc_id) else BaseType.find(assoc_id) end end end 我怎样才能使这个渴望加载 Whtvr.includes(:assoc).
class Whtvr
belongs_to :assoc, polymorphic: true
def assoc
if assoc_type == 'a_type'
AType.find(assoc_id)
else
BaseType.find(assoc_id)
end
end
end
我怎样才能使这个渴望加载
Whtvr.includes(:assoc).find(1, 2, 3)
我认为,即使getter没有被覆盖,也不能急于加载多态关联 您需要为每种类型的关联定义一个
所属的
belongs_to :a_type, class_name: "AType", foreign_key: "assoc_id"
belongs_to :base_type, class_name: "BaseType", foreign_key: "assoc_id"
然后,根据你想做什么,使用其中一种
Whtvr.includes(:a_type).find(1)
我认为应该是Whtvr.includes(:a_类型,:base_类型)。使用def assoc;a|U型|基|U型;结束
我不明白为什么你必须像那样覆盖getter。这样我就可以保留旧的界面,如果有脚本的话,我肯定会得到一个。看起来这对我,ty和gratz来说很有效,10k(: