title: Select values through many to many relationship in active record using "where"
body: <p>I'm trying to get all the distict brands that have a part belonging to the category named car (category.name = "car"), through a many to many relationship.</p>
  
  <p>Here are the models:</p>
  
  <pre><code>class Brand &lt; ActiveRecord::Base
    has_many :brand_parts
    has_many :parts, through: :brand_parts
  end
  </code></pre>
  
  <p>-</p>
  
  <pre><code>class Category &lt; ActiveRecord::Base
    has_many :parts
  end
  </code></pre>
  
  <p>-</p>
  
  <pre><code>class Part &lt; ActiveRecord::Base
    belongs_to :category
  
    has_many :brand_parts
    has_many :brands, through: :brand_parts
  end 
  </code></pre>
  
  <p>-</p>
  
  <pre><code>class BrandPart &lt; ActiveRecord::Base
    belongs_to :brand
    belongs_to :part
  end
  </code></pre>
  
  <p><strong><em>Update: Answer below nailed it</em></strong></p>
  
  <p>Here's This is what I've got so far:</p>
  
  <pre><code>Part.joins(:category).joins(:brands).where(category: {name: 'car'}).select(brands.name).distinct
  </code></pre>
  
  <p>The query it outputs (listed below) works GREAT in PG admin but through active record instead of being able to see the brand names, I just get a result set that looks like this:</p>
  
  <pre><code>#&lt;Part id: nil&gt;
  #&lt;Part id: nil&gt;
  #&lt;Part id: nil&gt;
  </code></pre>