0

In my app, each Game involves two players, who have different roles. One plays cat, the other plays dog. How can I describe this in Ruby's datamapper?

The documentation only gives examples where the names of the properties match the name of the class, which limits us to one association per class http://datamapper.org/docs/associations.html

I would like my game to have a cat player and a dog player.

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465

1 Answers1

1

The doc by your link has the answer. Read more thoroughly.

class Player
  include DataMapper::Resource
end

class Game
  include DataMapper::Resource
  belongs_to :cat, 'Player'
  belongs_to :dog, 'Player'
end

Update: you can use these associations in the Player model if you need

class Player
  include DataMapper::Resource
  has n, :cat_games, :child_key  => [ :cat_id ]
  has n, :dog_games, :child_key  => [ :dog_id ]
end
ujifgc
  • 2,215
  • 2
  • 19
  • 21