新手问题 请教这个查询应该怎么写?

stephen · September 30, 2014 · Last by stephen replied at October 01, 2014 · 1497 hits

game team user 三个对象!关系如下:

game ( team1_id team2_id )
game belongs_to team1
game belongs_to team2
team (user_id)
team belongs_to user
team has_many players  (player就是user)

现在要搜索出和当前用户有关系的所有比赛! 大概有四种关系 第一种:current_user 是 team1 的创建者 第二种:current_user 是 team2 的创建者 第三种:current_user 是 team1 的队员 第四种:current_user 是 team2 的队员

这种查询应该要怎么写?我暂时思路写得如下:

Game.joins(:team).where("team1.user_id = ? || team2.user_id = ? || team1.members_accepted.include(?) || team2.members_accepted.include()")

Game 与 team 之间是多对多的关系吧,用关系表来做是不是更好?

要么 rails,要么直接 sql。

@msg7086 不是多对多,team has_many games, game 没很多的 参加队伍,就两队而已

@liwei78 看过了,还是不是很明白怎么写

#4 楼 @stephen 我觉得,「2」,就是多的意思了……。

@msg7086 你误会了我的需求了

You need to Sign in before reply, if you don't have an account, please Sign up first.