title: Get associated records using joins
body: <p>I have Category, Book, Order Models</p>
  
  <p><strong>Category.rb</strong></p>
  
  <pre><code>has_many :books
  </code></pre>
  
  <p><strong>Book.rb</strong></p>
  
  <pre><code>belongs_to :category
  has_many :orders
  </code></pre>
  
  <p><strong>Order.rb</strong></p>
  
  <pre><code>belongs_to :book.
  </code></pre>
  
  <p>My task is get the orders got placed on specific category.</p>
  
  <p>I've tried these two options:</p>
  
  <p><strong>Using joins</strong></p>
  
  <pre><code>Order.joins(:book =&gt; :category).where('books.category_id = ?', 138)
  </code></pre>
  
  <p><strong>And using two queries:</strong></p>
  
  <ol>
  <li><p>To find out book_ids which comes under a specific category</p></li>
  <li><p>Find out orders which has those book_ids</p>
  
  <p>book_ids = Book.where(:category_id => 138).pluck(:id)</p>
  
  <p>Order.where(:book_id => book_ids).</p></li>
  </ol>
  
  <p>I wondering which one is correct. How can I get the orders got placed on specific category?</p>