Is there AR magic for this?

I am working on a game system. My models include User, Game, and Player:

User has_many Players
Game has_many Players
Player belongs_to User, Game

Basically, Player is a join table between User and Game, but since there
information that is specific to the relationship I want it to be a model
its own right. The part where I’d like some AR magic is that given a
(the id is in the session) and a Game (the id is part of the URL), I
to find the Player (assuming there is one) relating them. My current
implementation looks something like this (pseudocode):

@user = User.find(session[:user_id])
@game = Game.find(params[:id])
@player = @game.players.detect { |p| p.user_id = }

It works, and I’m likely to use the players list later anyway, but I
like there should be a simple way to get the Player without loading all
players. I suppose Player.find_by_game_id_and_user_id(,
would work, but it isn’t a whole lot nicer. I’m hoping for something
Player.find_by_owners(:user => @user, :game => @game).


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs