has_many :through
Rails/ActiveRecord でポリモーフィック アソシエーションをどのように熱心にロードしますか?
基本設定は次のとおりです。
class Post < ActiveRecord::Base
has_many :categorizations, :as => :categorizable
has_many :categories, :through => :categorizations
end
class Category < ActiveRecord::Base
has_many :categorizations, :as => :category
has_many :categorizables, :through => :categorizations
end
class Categorization < ActiveRecord::Base
belongs_to :category, :polymorphic => true
belongs_to :categorizable, :polymorphic => true
end
Rails 2.3.x と結合モデルでの二重ポリモーフィック アソシエーションのこのイーガー ロードの問題を解決したいと仮定すると、次の:through
ようなものでアソシエーションをどのようにイーガー ロードしますか?
posts = Post.all(:include => {:categories => :categorizations})
post.categories # no SQL call because they were eager loaded
それはうまくいきません、何かアイデアはありますか?