4
你如何在Rails/ActiveRecord中加載多態的has_many :through
關聯?Eager Load Polymorphic has_many:通過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
這不工作,有什麼想法?
這個例子並不多態。 – cicloon 2013-06-12 11:56:55