title: rails getting attributes of associated model for many objects in one query
body: <p>My title might be confusing, I wasn't sure what to write.</p>
  
  <p>In rails I understand how to fetch <strong>Many Objects</strong> for <strong>One parent object</strong></p>
  
  <pre><code>@first_user = User.first
  @first_user_posts = @first_user.posts
  </code></pre>
  
  <p>But how can I fetch <strong>Many Objects</strong> for <strong>Many parent objects</strong> and select its attributes in one query?. I am trying to do something like that:</p>
  
  <pre><code>@many_posts = Post.all
  @posts_by_user_gender = @many_posts.joins(:user).map(&amp;:gender)
  </code></pre>
  
  <p>hoping it would give me an array that could look something like this:</p>
  
  <pre><code>@posts_by_user_gender =&gt; ["male", nil, "female", nil]
  #I know I can do this map technique if I fetch it directly from the User model
  # User.all.map(&amp;:gender), 
  # but I want to start with those that posted in a specific category
  # Post.where(:category_id =&gt; 1) 
  </code></pre>
  
  <p>and then to count the males I could use the <a href="http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-count" rel="nofollow">Ruby Array method .count</a></p>
  
  <pre><code>@males_count_who_posted = @posts_by_user_gender.count("male")
  =&gt; 1
  </code></pre>
  
  <p>I could always do 3 separate queries </p>
  
  <pre><code>@males_count_who_posted = @many_posts.select(:user_id).joins(:user)
                                        .where("gender = ?", "male").count
  @females_count_who_posted = ...
  </code></pre>
  
  <p>but I find that extremely inefficient, especially if I do the same for something like "industry" where you could have more than 3 options. </p>