我有以下幾點:阿雷爾和多態的關聯連接
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
注意如何,我試圖加入與多態merchant_owner商戶,並獲得這樣的:的ActiveRecord :: EagerLoadPolymorphicError:不能急於加載多態性關聯:merchant_owner。 @merchants = Merchant.includes(:merchant_owner)最初起作用,但是當我開始遍歷視圖中的@merchants數組時,它打破了同樣的錯誤 - 似乎是這樣,因爲我們使用延遲加載的關係和只有當非Arel方法被調用時,它纔會到達DB。
任何想法? Arel是否支持多態關聯連接?任何解決方法?我可以下降到純粹的SQL,但這是小豬。
感謝
實際上,您可能希望通過在('Agent')中添加「and merchant_owner_type來保護該連接」 ,'經銷商','IPSP')「 – njorden 2012-06-18 21:09:24