title: Rails - insert many random items on create with has_many_through relation
body: <p>I want to create a random pack of 15 cards which should be invoked in the cardpacks_controller on create. I have the following models:</p>
  
  <p>Card:</p>
  
  <pre><code>class Card &lt; ActiveRecord::Base
    # relations
    has_many :cardpacks, through: :cardpackcards
    belongs_to :cardset
  end
  </code></pre>
  
  <p>Cardpack:</p>
  
  <pre><code>class Cardpack &lt; ActiveRecord::Base
    #relations
    has_many :cards, through: :cardpackcards
    belongs_to :cardset
  
    # accept attributes
    accepts_nested_attributes_for :cards
  end
  </code></pre>
  
  <p>Cardpackcards:</p>
  
  <pre><code>class Cardpackcard &lt; ActiveRecord::Base
    #relations
    belongs_to :card
    belongs_to :cardpack
  end
  </code></pre>
  
  <p>Cardsets:</p>
  
  <pre><code>class Cardset &lt; ActiveRecord::Base
    #relations
    has_many :cards
    has_many :cardsets
  end
  </code></pre>
  
  <p>How can I create 15 Cardpackcards records with random card_id values and with the same cardpack_id (so they belong to the same pack)</p>
  
  <p>I have watched the <a href="http://railscasts.com/episodes/73-complex-forms-part-1" rel="nofollow">complex form series</a> tutorial but it gives me no comprehension as how to tackle this problem.</p>
  
  <p>I hope anyone can help me solve this problem and give me more insight in the rails language.</p>
  
  <p>Thanks,
  Erik</p>