2016-04-27 1 views
1

私はプレイした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 

を私は達成するために必要なもの、ユーザーが簡単に参加リーグの間、ユーザーがリーグを変更した場合、彼はまだすべてのリーダーボードに表示されていることを切り替えることができるということです。ユーザーが今すぐリーグを変更した場合、彼は再び参加するまでリーダーボードにはいません。リーグIDが変更されてから正常です。

+0

缶ユーザーは多くのリーグでも? –

答えて

1

UserLeagueの間には、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 

これは、それぞれあなたのleaguesusersテーブルへの外部キーが含まれています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 
関連する問題