我正在创建一个应用程序来存储玩FIFA游戏并与朋友建立私人排行榜。
我设法将用户添加到联盟(私人组),但现在我想让用户加入多个联赛并轻松切换。
我为游戏和用户添加了一个league_id。
加载排行榜时,我只显示与current_user.league_id匹配的用户,对于胜负,我只计算与该匹配的游戏current_user.league_id。
这非常有效,但是用户应该可以加入其他联盟,并在他们之间轻松切换。我正在考虑为用户创建另一个字段,用于存储所有已加入联盟的集合,并添加一个操作来更改active_league_id。
有人能指出我在正确的方向吗?
class User < ActiveRecord::Base
devise :registerable, :confirmable
devise :omniauthable, :omniauth_providers => [:facebook]
#RELATIONS SINGLE GAMES
has_many :home_games, class_name: 'Game', foreign_key: 'home_team_user_id'
has_many :away_games, class_name: 'Game', foreign_key: 'away_team_user_id'
#RELATIONS MULTI GAMES
has_many :first_home_games, class_name: "Multiplayergame", foreign_key: "home_team_first_user_id"
has_many :second_home_games, class_name: "Multiplayergamer", foreign_key: "home_team_second_user_id"
has_many :first_away_games, class_name: "Multiplayergame", foreign_key: "away_team_first_user_id"
has_many :second_away_games, class_name: "Multiplayergame", foreign_key: "away_team_second_user_id"
#RELATIES SCORE CLASSEREN SINGLE GAMES
has_many :wins, class_name: 'Game', foreign_key: 'winner_id'
has_many :losses, class_name: 'Game', foreign_key: 'loser_id'
has_many :bonusses, class_name: 'Game', foreign_key: 'bonus_id'
has_many :loserbonusses, class_name: 'Game', foreign_key: 'bonus_loser_id'
has_many :firstdraws, class_name: 'Game', foreign_key: 'first_draw_id'
has_many :seconddraws, class_name: 'Game', foreign_key: 'second_draw_id'
#RELATIES SCORE CLASSEREN MULTI GAMES
has_many :firstwins, class_name: 'Multiplayergame', foreign_key: 'winner_first_id'
has_many :secondwins, class_name: 'Multiplayergame', foreign_key: 'winner_second_id'
has_many :firstlosses, class_name: 'Multiplayergame', foreign_key: 'loser_first_id'
has_many :secondlosses, class_name: 'Multiplayergame', foreign_key: 'loser_second_id'
has_many :firstbonusses, class_name: 'Multiplayergame', foreign_key: 'bonus_first_id'
has_many :secondbonusses, class_name: 'Multiplayergame', foreign_key: 'bonus_second_id'
has_many :firstloserbonusses, class_name: 'Multiplayergame', foreign_key: 'bonus_first_loser_id'
has_many :secondloserbonusses, class_name: 'Multiplayergame', foreign_key: 'bonus_second_loser_id'
has_many :firstmultidraws, class_name: 'Multiplayergame', foreign_key: 'first_multidraw_id'
has_many :secondmultidraws, class_name: 'Multiplayergame', foreign_key: 'second_multidraw_id'
has_many :thirdmultidraws, class_name: 'Multiplayergame', foreign_key: 'third_multidraw_id'
has_many :fourthmultidraws, class_name: 'Multiplayergame', foreign_key: 'fourth_multidraw_id'
belongs_to :league
has_one :league_admin, class_name: 'League', foreign_key: 'league_admin_id'
##############################################################################################
### TOTAL WINS CURRENT LEAGUE SINGLE PLAYER
def current_league_wins
wins.where(:league_id => self.league_id).count
end
#### TOTAL LOSSES CURRENT LEAGUE SINGLE PLAYER
def current_league_losses
losses.where(:league_id => self.league_id).count
end
#### TOTAL DRAWS CURRENT LEAGUE SINGLE PLAYER
def draws
firstdraws.where(:league_id => self.league_id).count + seconddraws.where(:league_id => self.league_id).count
end
#####################################################################################################
#### TOTAL WINS CURRENT LEAGUE MULTIPLAYER
def current_league_multi_wins
firstwins.where(:league_id => self.league_id).count + secondwins.where(:league_id => self.league_id).count
end
#### TOTAL LOSSES CURRENT LEAGUE MULTIPLAYER
def current_league_multi_losses
firstlosses.where(:league_id => self.league_id).count + secondlosses.where(:league_id => self.league_id).count
end
#### TOTAL DRAWS CURRENT LEAGUE MULTIPLAYER
def multidraws
firstmultidraws.where(:league_id => self.league_id).count + secondmultidraws.where(:league_id => self.league_id).count + thirdmultidraws.where(:league_id => self.league_id).count + fourthmultidraws.where(:league_id => self.league_id).count
end
控制器记分牌:
class ScoreboardController < ApplicationController
before_action :authenticate_user!
#LAAD ALLE USERS GERANSCHIKT VOLGENS SCORE
def index
@users = User.where(:league_id => current_user.league_id).sort_by(&:score).reverse
end
end
我需要实现的是,用户可以轻松地在加入的联赛之间切换,如果用户更改联赛,他仍会出现在所有排行榜中。如果一个用户现在改变了联赛,他就不再进入那个排行榜,直到他重新加入,这是正常的,因为他的league_id发生了变化。
答案 0 :(得分:1)
我认为您正在寻找的是User
和League
之间的has_and_belongs_to_many关系。
class User < ActiveRecord::Base
has_and_belongs_to_many :leagues
end
class League < ActiveRecord::Base
has_and_belongs_to_many :users
end
这将创建一个名为leagues_users
的表,它将分别包含leagues
和users
表的外键。
您可以通过查看current_user.leagues
轻松切换要查看的联赛。
修改强>
关于您的记分牌控制器,我说根据当前用户设置记分板更有意义(如果用户可以属于多个联赛)。
这样的事情:
class Scoreboard < ActiveRecord::Base
has_one :league
end
def ScoreboardController < ApplicationController
def show
@scoreboard = Scoreboard.find(params[:id])
# and access the users like so:
# @scoreboard.league.users
end
end