【问题标题】:Tournament -> Team -> Player Associations锦标赛 -> 球队 -> 球员协会
【发布时间】:2017-03-06 00:14:14
【问题描述】:

我是 Rails 的初学者,但我边走边学。我正在尝试创建一个锦标赛入口门户,一个团队将在其中输入给定锦标赛的球员。我已经阅读了一些关于关联的内容,但是在思考如何在这种情况下应用它们时遇到了一些麻烦。

作为基本概述:

  • 一场比赛,有很多球队。
  • 每支球队都有很多球员
  • 因此一场比赛也有很多球员(通过球队
    输入)

这是我的代码,但我不确定它是否正确,因为我无法获得与玩家关联的任何锦标赛 ID。

(锦标赛.rb)

class Tournament < ApplicationRecord
  has_many :teams
  has_many :players, :through => :teams  
end

(team.rb)

class Team < ApplicationRecord
  belongs_to :tournament
  has_many :players
end

(播放器.rb)

class Player < ApplicationRecord
  has_one :team
  has_one :tournament, :through => :team
end

在 Players 表中同时存在 team_id 和锦标赛 ID 字段,但是当我在控制台中尝试时,我只能通过关联填充 team_id 字段。

我想知道我的联想是否有问题。

【问题讨论】:

    标签: ruby-on-rails associations


    【解决方案1】:

    'belongs_to'、'has_many'、'has_one'的用法当然取决于数据库中的数据模型。

    如果players表中有team_id外键,则需要将Player类定义为:

    class Player < ApplicationRecord
      belongs_to :team
      has_one :tournament, :through => :team
    end
    

    另外,我真的相信Tournament Team应该有many-to-many的关联(如果队伍当然可以参加很多比赛的话)。我建议添加模型 TeamTournament 并将最终模型结构定义为:

    class Tournament < ApplicationRecord
      has_many :team_tournaments
      has_many :teams, :through => :team_tournaments
      has_many :players, :through => :teams
    end
    
    class TeamTournament < ApplicationRecord
      belongs_to :team
      belongs_to :tournament
    end
    
    class Team < ApplicationRecord
      has_many :team_tournaments
      has_many :tournaments, :through => :team_tournaments
      has_many :players
    end
    

    【讨论】:

    • 出于兴趣如何使用 TeamTournament 模型?我在这个地方的一些不同答案和示例中看到了类似的概念,但我无法理解使用/概念 - 可能缺乏经验。
    【解决方案2】:

    Player 类应该与 Team 和 Tournament 有belongs_to 关联

    class Player < ApplicationRecord
      belongs_to :team
      belongs_to :tournament
    end
    

    【讨论】:

    • belongs_to :tournament, :through =&gt; :team 不起作用
    • 'belongs_to' 与当前表上的 foreign_key 一起使用。除非“玩家”表包含对锦标赛的引用,否则锦标赛可以定义为 has_one :tournament, through: :team
    【解决方案3】:

    好的。我假设您的问题是关于您的模型关联,而不是如何建立关联以从 player 获取 tournament_id 等等。所以我会试着给你一些关于你的项目的提示,并且可以为它建立关联。

    当我了解您的门户想法时...您希望tournament 拥有许多teams,而team 拥有许多players。但是你想从player 得到tournament_id。我相信您不想这样做,因为在现实生活中的锦标赛确实可能“拥有”一些球员,但每个球员都不必属于某个锦标赛。他可以参加很多比赛。所以你不需要为此建立关联。锦标赛和球队也是如此。但既然球队有球员,他就必须属于那支球队。因此,您需要为此进行关联。

    为您完成我的设置如下:

    (锦标赛.rb)

    class Tournament < ActiveRecord::Base
      has_many :teams
    end
    

    (team.rb)

    class Team < ActiveRecord::Base
      has_many :players
    end
    

    (播放器.rb)

    class Player < ActiveRecord::Base
      belongs_to :team
    end
    

    还有一个关于如何获得tournament 的示例,其中某些team 在没有直接关联的情况下参与:

    team = Team.first # just take some team
    Tournament.includes(:teams).where(teams: { id: team.id })
    

    您可以通过同样的方式实现其他目标(获得某个玩家所属的锦标赛等)。但这种情况不需要关联。当对象在概念上与另一个对象相关时,需要关联。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      • 2012-08-22
      • 1970-01-01
      相关资源
      最近更新 更多