Trouble on eager loading “second degree” associated objects

无人久伴 提交于 2019-12-04 01:55:04

问题


I am running Ruby on Rails 3.1. I would like to eager loading "second degree" associated objects by applying some conditions, but I am in trouble.

It seems that I already solved part of my issue by using:

article_categories =
  article
    .categories
    .includes(:comments => [:category_relationships])
    .where(:category_relationships => {:user_id => @current_user.id})

where involved classes are stated as the following:

class Category < ActiveRecord::Base
  has_many :comment_relationships
  has_many :comments,
    :through => :comment_relationships

  ...
end

class Comment < ActiveRecord::Base
  has_many :category_relationships
  has_many :categories,
    :through => :category_relationships

  ...
end

The above code (it seems to do it right):

  1. loads all categories by caring the has_many :through :category_relationships association (that is, by caring the .where(:category_relationships => {:user_id => @current_user.id}) condition);
  2. eager loads all article.comments.where(:user_id => @current_user.id).

However, I would like to make some more:

  1. to order retrieved categories by a :position attribute present in category_relationships so that the resulting article_categories are ordered by position;
  2. to eager load also category_relationship objects where user_id == @current_user.id since the above code doesn't make that.

How can I make that by taking advantage from the eager loading?


回答1:


The solution:

  1. .order("category_relationships.position")

  2. Imagine eager loading is cartessian product with some filtering so "where" is filtering the end result of include (left join really). But it can be done with where with subquery which first will filter categories by user then your where can be removed.



来源:https://stackoverflow.com/questions/9629860/trouble-on-eager-loading-second-degree-associated-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!