Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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 钢轨预加载多态关联_Ruby On Rails_Ruby On Rails 4_Activerecord_Polymorphic Associations - Fatal编程技术网

Ruby on rails 钢轨预加载多态关联

Ruby on rails 钢轨预加载多态关联,ruby-on-rails,ruby-on-rails-4,activerecord,polymorphic-associations,Ruby On Rails,Ruby On Rails 4,Activerecord,Polymorphic Associations,我有以下模型关系: class Customer < ActiveRecord::Base has_many transactions end class Transaction < ActiveRecord::Base belongs_to :customer belongs_to :ta_type, polymorphic: true end class Downpament < ActiveRecord::Base has_one :trans

我有以下模型关系:

class Customer < ActiveRecord::Base
   has_many transactions
end
class Transaction < ActiveRecord::Base
   belongs_to :customer
   belongs_to :ta_type, polymorphic: true
end 

class Downpament < ActiveRecord::Base
   has_one :transaction, as: :ta_type
end 
 class Installemnt < ActiveRecord::Base
   has_one :transaction, as: :ta_type
end 
但我需要一个关于客户的条件。下面的查询也在运行

transactions = self.includes(customer:[:location]).
    where(customer: {office_id: office_id})
然而,这两者的组合不起作用

transactions = self.includes(:ta_type, customer:[:location]).
    where(customer: {office_id: office_id})
休息和投掷

Completed 500 Internal Server Error in 113ms (ActiveRecord: 3.1ms)
ActiveRecord::EagerLoadPolymorphicError (Cannot eagerly load the  polymorphic association :ta_type):
我发现了一篇很好的帖子,描述了我遇到的问题。但我无法让它工作

我试着使用一个分离的连接和一个预加载以及一个连接来产生相同的异常

 transactions = self.
     preload(:ta_type, customer:[:location]]).
     joins(:ta_type).
     where(customer: {office_id: office_id})
preload\u associations
方法似乎是个好主意,但它已从rails 3.0.9中删除


如何才能将多态关联与条件一起正确预加载?

的可能重复项您可以告诉我们
office\u id
。它是事务的列还是外部的某个变量?
 transactions = self.
     preload(:ta_type, customer:[:location]]).
     joins(:ta_type).
     where(customer: {office_id: office_id})