title: Rails - how to query the database to obtain all types of products in a specific category
body: <p>I have 3 models, Type, Category &amp; Product. I am building an ecommerce platform and on the main products page I am able to list all of the many products of a particular category (Mens,Womens, Tshirts, Hats). </p>
  
  <p>What I would like to do now is loop through all the product types tied to a specific category. The product types would be for the Mens category(Tanks, Tees, Shirts), and Womens category would have (Dresses, Tanks, Shirts), Hats would be (Flexfit, Trucker, Camper) and Hoodies would be (Pullover, Zipup). </p>
  
  <p>A sample output would be
  For Mens => Tanks, Tees, Shirts.</p>
  
  <p>I am not sure if this would be a belongs to has many relationship between type and category, so the code in Type.rb might not be correct. I would like to know how to query the database to find all types of products in a category and also how to input them inside my seeds file.</p>
  
  <p>Thanks!</p>
  
  <p>Here is my code.</p>
  
  <p>Category.rb</p>
  
  <pre><code>class Category &lt; ActiveRecord::Base
   has_many :categorizations
   has_many :products, through: :categorizations
   has_many :types
  end
  </code></pre>
  
  <p>Product.rb</p>
  
  <pre><code>class Product &lt; ActiveRecord::Base
   has_many :options, dependent: :destroy
   has_many :images, dependent: :destroy
  
   has_many :categorizations
   has_many :categories, through: :categorizations
  
   has_many :types
  
  
  
   def image_url
    self.images.first.url
   end
  
   def has_image?
    self.images.exists?
   end
  
  end
  </code></pre>
  
  <p>Type.rb</p>
  
  <pre><code>class Type &lt; ActiveRecord::Base
   belongs_to :product
   belongs_to :category
  end
  </code></pre>
  
  <p>categories_controller.rb</p>
  
  <pre><code>class CategoriesController &lt; ApplicationController
  
  def show
   @products = Product.includes(:categories).where('categories.name' =&gt; params[:name])
   ap @products
  
  render :layout =&gt; 'application_categories'
  end
  end
  </code></pre>