简体   繁体   English

Arel和多态关联加入

[英]Arel and polymorphic association joins

I have the following: 我有以下内容:

class BaseReseller < ActiveRecord::Base

  set_table_name "resellers"

  acts_as_nested_set

  has_many :merchants, :as => :merchant_owner, :dependent => :destroy
end

class IPSP < BaseReseller
end    

class Agent < BaseReseller
end

class Reseller < BaseReseller
end

class Merchant < ActiveRecord::Base
  belongs_to :merchant_owner, :polymorphic => true
end

class MerchantsController < ApplicationController
  ... 
  def index
    ...
    @merchants = Merchant.joins(:merchant_owner) # breaks!
  end
end

Notice how I am trying to join the Merchant with the polymorphic merchant_owner and get this: ActiveRecord::EagerLoadPolymorphicError: Can not eagerly load the polymorphic association :merchant_owner . 请注意我是如何尝试使用多态的merchant_owner加入Merchant并获取此信息: ActiveRecord :: EagerLoadPolymorphicError:无法急切加载多态关联:merchant_owner

@merchants = Merchant.includes(:merchant_owner) works initially, but when I start iterating over the @merchants array in the views, it breaks with the same error - seems to be so because we work with lazily loaded relations and only when a non-Arel method is invoked, it actually goes to the DB. @merchants = Merchant.includes(:merchant_owner)最初工作,但是当我开始迭代视图中的@merchants数组时,它会打破相同的错误 - 似乎是因为我们使用延迟加载的关系并且仅当非-Arel方法被调用,它实际上转到了DB。

Any ideas? 有任何想法吗? Does Arel support polymorphic association joins? Arel是否支持多态关联连接? Any workarounds? 任何解决方法? I can drop down to pure SQL but this is piggy. 我可以下载到纯SQL,但这是小猪。

Thanks 谢谢

This may be too much to ask of Arel, I'm not sure of a great solution. 这对Arel来说可能太过分了,我不太确定一个很好的解决方案。 In your case it looks like all of the merchant_owners share the same reseller table, so it makes some sense to want to join from the polymorphic association, but in general the polymorphic associations can relate to various models (for different tables) throughout your data model. 在您的情况下,看起来所有的merchant_owners都共享相同的经销商表,因此想要从多态关联中加入是有道理的,但通常多态关联可以与整个数据模型中的各种模型(针对不同的表)相关联。 This would get pretty ugly in SQL and probably pretty slow in the general case which is why I expect EagerLoadPolymorphicError exists. 这在SQL中会变得相当丑陋,并且在一般情况下可能相当慢,这就是我期望EagerLoadPolymorphicError存在的原因。 For your situation, it could make sense to work around with something like: 根据您的情况,可以使用以下方法解决问题:

Merchant.joins("INNER JOIN resellers on merchant_owner_id = resellers.id")

of course it would be cleanest for this join to be a scope on your model so you could just do Merchant.joined_with_owners and the SQL relegated to the model layer. 当然,将此连接作为模型的范围是最干净的,因此您可以只执行Merchant.joined_with_owners并将SQL降级到模型层。 This wouldn't however work if other, non reseller models have many merchants. 然而,如果其他非经销商型号拥有许多商家,则这不会起作用。

I'm a bit surprised that you're seeing the same error with Merchant.includes(:merchant_owner), I don't see that with similar data even as I access various attributes but could be due to various differences. 我有点惊讶你看到与Merchant.includes(:merchant_owner)相同的错误,即使我访问各种属性,我也没有看到类似的数据,但可能是由于各种差异。

Are trying to eager load for performance reasons or because you have additional Arel where clauses that depend on reseller attributes? 是出于性能原因还是因为你有额外的Arel where依赖于经销商属性的子句? If it's for performance, you may be best falling back on SQL... 如果它是为了性能,你可能最好回到SQL ...

Problem was a missing column in one of the tables. 问题是其中一个表中缺少的列。 Rails does support polymorphic associations between STI hierarchies extremely well, but be careful regarding the correct columns for polymorphism, STI, etc. Rails确实非常支持STI层次结构之间的多态关联,但是要注意多态性,STI等的正确列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM