title: Setting up has_many through correctly in Rails
body: <p>Yesterday I asked a question about setting up has_and_belongs_to_many relationships between models in Rails. I eventually got those relationships working, however, I realized that I needed to store additional information in my join table, which means I should be using a has_many through relationship with a join model (I presume?). </p>
  
  <p>I now have a Recipe model, Ingredient model, and an Amounts join model. Each Recipe has many Ingredients and vice versa. And each Ingredient in a recipe has a certain Amount in a certain unit type. </p>
  
  <p>Here is my setup so far:</p>
  
  <p>models/recipe.rb</p>
  
  <pre><code>class Recipe &lt; ActiveRecord::Base
    has_many :amounts
    has_many :ingredients, :through =&gt; :amounts
  end
  </code></pre>
  
  <p>models/ingredient.rb</p>
  
  <pre><code>class Ingredient &lt; ActiveRecord::Base
    has_many :amounts
    has_many :recipes, :through =&gt; :amounts
  end
  </code></pre>
  
  <p>models/amounts.rb</p>
  
  <pre><code>class Amounts &lt; ActiveRecord::Base
    belongs_to :recipes
    belongs_to :ingredients
  end
  </code></pre>
  
  <p>MySQL tables:</p>
  
  <pre><code>$ mysql &gt; show columns from recipes;
  +------------+--------------+------+-----+---------+----------------+
  | Field      | Type         | Null | Key | Default | Extra          |
  +------------+--------------+------+-----+---------+----------------+
  | id         | int(11)      | NO   | PRI | NULL    | auto_increment |
  | name       | varchar(255) | NO   |     | NULL    |                |
  | desc       | text         | NO   |     | NULL    |                |
  | image_url  | varchar(255) | YES  |     | NULL    |                |
  | created_at | datetime     | NO   |     | NULL    |                |
  | updated_at | datetime     | NO   |     | NULL    |                |
  +------------+--------------+------+-----+---------+----------------+
  
  mysql&gt; show columns from ingredients;
  +------------+--------------+------+-----+---------+----------------+
  | Field      | Type         | Null | Key | Default | Extra          |
  +------------+--------------+------+-----+---------+----------------+
  | id         | int(11)      | NO   | PRI | NULL    | auto_increment |
  | name       | varchar(255) | NO   |     | NULL    |                |
  | created_at | datetime     | NO   |     | NULL    |                |
  | updated_at | datetime     | NO   |     | NULL    |                |
  +------------+--------------+------+-----+---------+----------------+
  
  mysql&gt; show columns from amounts;
  +---------------+--------------+------+-----+---------+----------------+
  | Field         | Type         | Null | Key | Default | Extra          |
  +---------------+--------------+------+-----+---------+----------------+
  | id            | int(11)      | NO   | PRI | NULL    | auto_increment |
  | recipe_id     | int(11)      | NO   |     | NULL    |                |
  | ingredient_id | int(11)      | NO   |     | NULL    |                |
  | amount        | int(11)      | NO   |     | NULL    |                |
  | units         | varchar(255) | NO   |     | NULL    |                |
  | created_at    | datetime     | NO   |     | NULL    |                |
  | updated_at    | datetime     | NO   |     | NULL    |                |
  +---------------+--------------+------+-----+---------+----------------+
  </code></pre>
  
  <p>I think I have the relationships set up correctly...I'm just unsure about how to add to the join model/table correctly and then access those values. For instance, will I be able to call @recipe.ingredients and return all the ingredients associated? </p>
  
  <p><strong>EDIT:</strong>
  I apologize for the confusion over what I am trying to do.</p>
  
  <p>When I was using the has_and_belongs_to_many relationship, I could push to @recipe.ingredients with '&lt;&lt;' and have the join table automatically inserted with the recipe_id and ingredient_id. However, this method doesn't seem to work when using has_many through because of the additional attributes in the join table.</p>
  
  <p>How would I go about creating these associations?</p>