title: Multiple References Between 2 Tables, one-to-many as well as many-to-many
body: <p>I'm just started learning RoR and decided to create a side project to learn more. I've been stuck on this problem for about a week now :(</p>
  
  <p>Basically, I'm trying to set up relationships between 2 tables, Users and Recipes.</p>
  
  <p>2 relationships need to form for the purpose of my app.</p>
  
  <ol>
  <li>a recipe is created by a user so it's a one-to-many association. </li>
  <li>a user can have favourite recipes and a recipe can be favourited by multiple users. So, this is a many-to-many association. </li>
  </ol>
  
  <p>This is what I've done so far. </p>
  
  <p>user model</p>
  
  <pre><code>  has_many :recipes
    has_many :user_recipes
    has_many :recipe, through: :user_recipes
  </code></pre>
  
  <p>recipe Model</p>
  
  <pre><code>  belongs_to :user, dependent: :destroy
    has_many :user_recipes
    has_many :users, through: :user_recipes
  </code></pre>
  
  <p>user_recipes model</p>
  
  <pre><code>  belongs_to :user
    belongs_to :recipe
  </code></pre>
  
  <p>user_recipe migration file</p>
  
  <pre><code>  t.belongs_to :recipe, index: true
    t.belongs_to :user, index: true
  </code></pre>
  
  <p>recipe migration file</p>
  
  <pre><code>  t.belongs_to :user
  </code></pre>
  
  <p>user migration file has nothing related to recipes.</p>
  
  <p>From what I did. I was able to: 
  1. assign a user to a recipe (the creator) and check using <code>recipe.user</code>
  2. assign favourite relationships using <code>recipe.users &lt;&lt; user</code></p>
  
  <p><strong>The problem</strong>
  when I type <code>user.recipes</code> it tells me which recipes a user has created and not which recipes a user likes. I think there should be a better way to set my associates up to be able to use all functionalities properly. </p>
  
  <p>I'd appreciate all the help. Thank you in advance!</p>