2016-04-27 16 views
1

我正在創建一個應用程序來存儲玩FIFA遊戲並與朋友建立私人排行榜。讓用戶加入多個聯盟(組)並在它們之間切換

我設法將用戶添加到聯賽(私人團體),但現在我希望讓用戶加入多個聯賽和兩者之間輕鬆切換。

我添加了一個league_id到遊戲和用戶。

當裝載排行榜我只顯示匹配current_user.league_id的用戶和勝和損失,我只計算與匹配current_user.league_id遊戲。

這完美的作品,但是用戶應該能夠加入annother聯賽並在它們之間輕鬆切換。我正在考慮爲用戶存儲所有聯合聯賽的集合並添加一個操作來更改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將分別包含外鍵的leaguesusers表。

您可以輕鬆地你想通過看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