您如何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
这不起作用,有什么想法吗?