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):
class DeckCard < ActiveRecord::Base
belongs_to :card
belongs_to :deck
end
These DeckCards will eventually have attributes reflecting position; such as in discard pile, in draw pile, etc.
A Deck model with
belongs_to :game
has_many :deck_cards
I'm trying to inialize the deck and am having various problems. I'm trying the following in Deck
def initialize
@cards = Card.find(:all)
@cards.each do |card|
# eventually another loop here on copies_in_deck
@deck_cards.build(card)
end
end
When I do Deck.new
I get an error on nil.build. Why is @deck_cards nil?
I'm using InstantRails2, which has rails 2.0.2. I found this article about creating multiple models in one action, so I tried using Deck.create! instead, and got an error about the wrong number of arguments.
Any suggestions?