I have three classes that need to be related, and I’m not sure how to do it.
The User class, is of course for a user, Game represents a game, GameTurn represents a turn in the game. Game belongs to a user, the user who initiated the game, and GameTurn belongs to a user, the user who played that turn.
What I want to do is associate User and Game through GameTurn, so I can quickly get a list of players in a Game, and a list of Games that a player played in. I’m just not sure how to do this, and don’t want to mess anything up since Game already belongs to a user. (properties not related were removed where possible, to make the definitions more readable).
Here are the relevant class definitions
class User
include DataMapper::Resource
property :id, Serial
property :name, String
property :created_at, DateTime
property :password, BCryptHash
has n, :gameTurns
end
class Game
include DataMapper::Resource
property :id, Serial
property :created_at, DateTime
property :finished, Boolean, :default=>false
property :finished_at, DateTime
property :length, Boolean #Number of turns
has n, :gameTurns
end
class GameTurn
include DataMapper::Resource
property :id, Serial
property :isText, Boolean
property :text, String
property :image, Text
belongs_to :game
belongs_to :user
end
I’m pretty inexperienced with ORMs in general, so if any of these associations can be better done another way, please let me know.
Thanks
I’ve never used
datamapper, but I believe what you need is something like