Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails多态关联覆盖了具有急切加载的getter_Ruby On Rails_Ruby_Ruby On Rails 3_Polymorphic Associations - Fatal编程技术网

Ruby on rails Rails多态关联覆盖了具有急切加载的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).

我有一些设计很糟糕的东西。它与重写的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).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(: