Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

ruby on rails - Eager loading: The right way to do things

I am running Ruby on Rails 3.1. I read the following articles and documentations about eager loading and I would like to find a right way to do things:

  1. Eager Loading Associations [Official documentation]
  2. ActiveRecord::Associations::ClassMethods (see the section "Eager loading of associations") [Official documentation]
  3. Eager loading [Blog article]

The #2 says:

Note that using conditions like Post.includes([:author, :comments]).where(['comments.approved = ?', true]).all can have unintended consequences.

The #3 says that those unintended consequences are (note: examples are pretty the same so I quote the exact text of the blog article but you have to keep in mind the workaround, not the specific implementation):

This query, since it would use a LEFT JOIN, would also discard all posts without a comment with the word “first” on any of it’s comments.

That is, if there are non "associated" objects, the "main associated" object will not be loaded. This is what happens when I try to use eager loading by adding some condition like .where(:category_relationships => {:user_id => @current_user.id}) in a my previous question, but I do not want that to happen.

So (defeatist because I probably can not use the eager loading in my case where condition can not be set in the has_many statement - note that in the above code the @current_user.id is "set dynamically" unlike in examples present in mentioned sites), I would like to know if there are pratiques/techniques/strategies so to limit database queries since I have a "N + 1 problem".

Maybe those pratiques/techniques/strategies are implementable by using the Ruby on Rails framework at all... the #1 says:

Even though Active Record lets you specify conditions on the eager loaded associations just like joins, the recommended way is to use joins instead.

What and how to solve this issue the right way?


Maybe a solution is to retrieve and build myself what needs to be loaded by running specific and separated database queries, but then the problem would be how to "pass" / "associate" / "interpolate" those retrieved "associated" objects to the "main associated" object so that those can be used "a là eager loading" way? That is, how to make possible (see the mentioned question for more information) to use code like @article.comments and get only comments that I eager loaded myself? 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...