title: Select multiple values from associated model rails
body: <p>Assuming I have this association</p>
  
  <pre><code>User have_many posts
  Post belongs_to user
  
  User   Post
  ----------------
  id     id
  name   title
         user_id
  </code></pre>
  
  <p><strong>How to list only post title and username with includes/joins ?</strong></p>
  
  <p>(list of posts [title - username])</p>
  
  <pre><code>@posts = Post.includes(:user).select('........')
  </code></pre>
  
  <p>don't offer this</p>
  
  <pre><code>@posts = Post.all.each {|p| p.user.username}
  </code></pre>
  
  <p><strong>__________________UP_____________________</strong></p>
  
  <p>It worked for joining 2 tables.</p>
  
  <p>What if I want to use it for more complex example?</p>
  
  <p>check out my prev question <a href="https://stackoverflow.com/questions/27756532/optimize-sql-query-rails">optimize sql query rails</a></p>
  
  <p>@Humza's answer partly worked.
  it might be something like this </p>
  
  <pre><code>@posts = Post.joins(:user, :category).paginate(:page =&gt; params[:page]).order("created_at DESC")
  </code></pre>
  
  <p>but It doesn't display posts that don't have category</p>
  
  <p>I also need to display gravatar but I think I can just use user.email as usr_email and use gravatar_for (post.usr_email) but I'll have to customize gravatar helper for this.</p>
  
  <p><strong>posts_controller.rb</strong></p>
  
  <pre><code>def index
    @posts = Post.includes(:user).includes(:comments).paginate(:page =&gt; params[:page]).order("created_at DESC")
  end
  </code></pre>
  
  <p><strong>index.html.erb</strong></p>
  
  <pre><code> &lt;%= render @posts %&gt;
  </code></pre>
  
  <p><strong>_post.html.erb</strong></p>
  
  <pre><code>&lt;%= gravatar_for post.user, size:20 %&gt;
  &lt;%= link_to "#{post.title}", post_path(post) %&gt;
  &lt;%= time_ago_in_words(post.created_at) %&gt; 
  &lt;%= post.comments.count %&gt;
  &lt;%= post.category.name if post.category %&gt;
  </code></pre>