title: Rails. Multiple instances of same foreign key in a single table
body: <p>I'm not sure how to model the following requirements:</p>
  
  <p>We send packs of 3 different beers to our users. The users are divided in 4 categories of tasting_profiles. Then, we prepare 4 different packs of beers for all our users. Then the users rate the beers and this will happen in a periodic basis.</p>
  
  <p><strong>NOTE: My main concern is that I have a table (Packs) that may have multiple instances of the same foreign key. Every pack consists of 3 kinds of beers.</strong></p>
  
  <p>Currently I have the following models:</p>
  
  <p>User / Beer / Profile / Pack  / rating</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    belongs_to :profile 
    has_many :ratings # A user rates every beer received.
    has_many :beers, through: :ratings
  end
  
  class Beer &lt; ActiveRecord::Base
    has_many :ratings
    has_many :users, through: :ratings
    has_many :packs
  end
  
  class Profile &lt; ActiveRecord::Base
    has_many :packs #we setup and send periodic packs for each tasting_profile (Profile)
    has_many :users #each tasting profile has many users
    has_may :beers #each tasting profile has many possible beers
  end
  
  class Pack &lt; ActiveRecord::Base
    belongs_to :beer #Not sure
    belongs_to :profile
  end
  
  class Rating &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :beer
  end
  </code></pre>
  
  <p><strong>The pack model question:</strong>
  My first option was to have the following fields in the Pack model:</p>
  
  <ol>
  <li>pack_id</li>
  <li>profile_id (FK)</li>
  <li>beer_id (FK1)</li>
  <li>beer_id (FK2)</li>
  <li>beer_ip (FK3)</li>
  <li>delivery_month</li>
  </ol>
  
  <p>This way I have one complete pack for entry.</p>
  
  <p>Researching here I read that apparently this is a bad practice and only one FK for entrance was suggested. Something like this:</p>
  
  <ol>
  <li>pack_id</li>
  <li>profile_id (FK)</li>
  <li>beer_id (FK)</li>
  <li>delivery_month</li>
  </ol>
  
  <p>In this case, i would have 3 entries for each pack. </p>
  
  <p>Finally I was considering to study an array field (not sure if it can be done with Foreign keys):</p>
  
  <ol>
  <li>pack_id</li>
  <li>profile_id (FK)</li>
  <li>beers [beer_id1 , beer_id2, beer_id3] # or hash [beer_1: beer_id, beer_2:..]</li>
  <li>delivery_month</li>
  </ol>
  
  <p>After a pack is created I need to populate the rating table with as many of the following entries as needed according to the number of users. If I have 100 users in the tasting_profile, I will send the 3 beers pack to them, resulting in 300 entries here </p>
  
  <ol>
  <li>rating_id</li>
  <li>User_id (FK)</li>
  <li>beer_id (FK)</li>
  <li>rate</li>
  <li>delivery_date</li>
  </ol>
  
  <p>I'm really confused about how to properly model this. ¡And I would thank any possible help! I tried to be as detailed as possible. Please let me know if you need further clarification.</p>
  
  <p>¡¡Thanks in advance!!</p>