我有以下内容,每个玩家都有很多游戏,每个游戏都有很多玩家,玩家可以选择是否参加游戏。
Game
has_many :shows
has_many :players, :through => :shows
Player
has_many :shows
has_many :games, :through => :shows
Show Migration
t.references :game
t.references :player
t.boolean :going, :default => false
如果玩家决定参加比赛,那么我想要做的就是设定为什么,最好的办法是什么?
答案 0 :(得分:1)
假设您知道玩家的ID(player_id
)和特定游戏的ID(game_id
),您可以执行以下操作:
Show.where('player_id = ? and game_id = ?', player_id, game_id).first.update_attributes(:going => true)
这更详细,但也可能:
player = Player.find(player_id)
show = player.shows.find_by_game_id(game_id)
show.update_attributes(:going => true)
如果你想迭代游戏,你可以这样做:
player = Player.find(id_of_player)
player.shows.each do |show|
if show.game == ... # condition that decides whether player's going or not
show.update_attributes(:going => true)
end
end