Colonel Panic
Colonel Panic

Reputation: 137584

Multiple many to one associations

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.

Upvotes: 0

Views: 189

Answers (1)

ujifgc
ujifgc

Reputation: 2255

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

Upvotes: 1

Related Questions