Eager loading: The right way to do things

风流意气都作罢 提交于 2019-11-28 18:21:38

After my eager loading, is it possible / correct to make something like @article.comments = my_eager_loaded_comments so to "pass"/"associate"/"interpolate" comments to articles?

Yes, it is possible. I do this regularly.

Note that my solution still retrieves ALL the associated objects from the DB. I don't think there is any solution to retrieving just the filtered association objects if your condition is dynamic. My solution focuses on the filtering of the retrieved association objects.

I am assuming the requirement is to get a list of articles and in each article, eager load the comments of only one particular user.

In the Article model:

def self.get_articles_with_comments_of_user( article_ids, user_id )
  articles = Article.where( id: article_ids ).includes( :comments )

  articles.each do |article|
    filtered_comments = article.comments.select { |comment| comment.user_id == user_id }
    article.association("comments").target = filtered_comments
  end

  articles
end

In the collection of articles returned by the above method, the comments association will have only the comments of that particular user.

I hope this is what you are asking for.

From #2 ActiveRecord::Associations::ClassMethods comes this:

If you do want eager load only some members of an association it is usually more natural to include an association which has conditions defined on it:

class Post < ActiveRecord::Base
  has_many :approved_comments,
    :class_name => 'Comment', 
    :conditions => ['approved = ?', true]
end

Post.includes(:approved_comments)

This will load posts and eager load the approved_comments association, which contains only those comments that have been approved.

If I understand this correctly in context, there's an ambiguity when you apply .where() to your main Query with an includes(), and AR applies the where to the whole query limiting your principle results to those that have qualifying associated predicates. But if you scope the predicate to just the association as above, AR understands and gives you all your principle results and those associated objects which match their predicate condition.

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