title: Rails optimal query to find all articles with has_many through
body: <p>I'm struggling to find all articles by knowing only categories users have subscribed to. Each articles can have many categories they belong to, my models look like</p>
  
  <p>Article:</p>
  
  <pre><code>class Article &lt; ActiveRecord::Base
  
  has_many :article_categories
  has_many :categories, through: :article_categories
  </code></pre>
  
  <p>Category:</p>
  
  <pre><code>class Category &lt; ActiveRecord::Base
  
  has_many :article_categories
  has_many :articles, through: :article_categories
  </code></pre>
  
  <p>ArticleCategory:</p>
  
  <pre><code>class ArticleCategory &lt; ActiveRecord::Base
    belongs_to :article
    belongs_to :category
  </code></pre>
  
  <p>article_categories table is just a storage for Article Categories with two columns: article_id &amp;&amp; category_id</p>
  
  <p>So how do I make proper query, hopefully with AR, if I have array of categories id's:</p>
  
  <pre><code>@ids = @categories.map { |c| c.id }
  </code></pre>