Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to model and create a custom deck of cards in rails?
    text
    copied!<p>I'm trying to model a card game in order to learn Rails. This is different than a standard deck of playing cards in that there can be multiple copies of a card in the deck. I'm running into problems while trying to initialize the deck. So far I've got a basic Card model with various attributes (such as copies_in_deck) but no associations. A DeckCard model which represents the cards in the deck (this is due to having multiples of the same card in the deck):</p> <pre><code>class DeckCard &lt; ActiveRecord::Base belongs_to :card belongs_to :deck end </code></pre> <p>These DeckCards will eventually have attributes reflecting position; such as in discard pile, in draw pile, etc.</p> <p>A Deck model with </p> <pre><code>belongs_to :game has_many :deck_cards </code></pre> <p>I'm trying to inialize the deck and am having various problems. I'm trying the following in Deck</p> <pre><code>def initialize @cards = Card.find(:all) @cards.each do |card| # eventually another loop here on copies_in_deck @deck_cards.build(card) end end </code></pre> <p>When I do <code>Deck.new</code> I get an error on nil.build. Why is @deck_cards nil?</p> <p>I'm using InstantRails2, which has rails 2.0.2. I found <a href="http://zargony.com/2007/09/17/vanishing-records-on-creating-multiple-models-in-one-action/" rel="nofollow noreferrer" title="this link">this article </a> about creating multiple models in one action, so I tried using Deck.create! instead, and got an error about the wrong number of arguments.</p> <p>Any suggestions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload