Eager loading: The right way to do things

前端 未结 2 2033
星月不相逢
星月不相逢 2020-12-13 14:59

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:

2条回答
  •  眼角桃花
    2020-12-13 15:30

    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.

提交回复
热议问题