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