rubydatamapperruby-datamapper

Find games involving player


In my models there is a many to many relation ship between games and users. How can I find all the games involving a given player?

I tried Game.all(Game.users.include?(u)) but got a NoMethodError about include?


Here are my models per http://datamapper.org/docs/associations.html

class User
    include DataMapper::Resource

    property :id,         Serial

    has n, :games, :through => Resource
end

class Game
    include DataMapper::Resource

    property :id,         Serial

    has n, :users, :through => Resource
end

Solution

  • Assuming you have an instance of a user u, then what you want is u.games.all. Each user has a set of games. I assume it is the games they are playing.