Rails eager loading of counts?

后端 未结 5 1016
暖寄归人
暖寄归人 2021-02-02 14:50

Eager loading is nice with the include attribute

Post.find(:all, :include => :author)

I\'m wondering if you can also eager load counts, like

相关标签:
5条回答
  • 2021-02-02 15:10

    I just ran into this challenge and solved it this way:

    def trainee_counts
       @trainee_counts ||= Hash[Trainee.group(:klass_id).count]
    end
    
       # where the count is needed
       trainee_counts[klass_id].to_i
    

    One call to database and does not load trainees.

    0 讨论(0)
  • 2021-02-02 15:14

    they should already be loaded use

    post.comments.length
    

    I was having this same problem because I was using .count

    0 讨论(0)
  • 2021-02-02 15:17

    Try this:

    Comment.count(:group => :post)
    

    To filter by conditions:

    Comment.count(:group => :post, :conditions => {:approved => true })
    

    These will return hashes with posts as keys and the number of comments as values.

    0 讨论(0)
  • 2021-02-02 15:23

    Building off of avaynshtok's answer, the following technique should just make 2 database calls.

    # ./app/controllers/posts_controller.rb
    
    def index
      # First load the posts
      @posts = Post.all
    
      # Then you can load a hash of author counts grouped by post_id
      # Rails 4 version:
      @comment_counts = Comment.group(:post_id).count
      # Rails 3 version:
      # @comment_counts = Comment.count(:group => :post_id)
    end
    

    Then in your view

    <!-- ./app/views/posts/index.html.erb  -->
    
    <% @posts.each do |post| %>
      <!-- reference the count by the post.id -->
      post_count: <%= @comment_counts[post.id] %>
    <% end %>
    
    0 讨论(0)
  • 2021-02-02 15:33

    In MySQL at least, it is faster to do these as two separate calls because you get to avoid the join. I know this doesn't answer your question, but it seems like you're trying to get better speed and doing

    Post.find ...
    

    Then

    post.comments.count
    

    Is faster and more memory efficient (for the database) than if you retrieve both in one query.

    0 讨论(0)
提交回复
热议问题