首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将我的模型关联到逃生房间管理rails应用程序?

如何将我的模型关联到逃生房间管理rails应用程序?
EN

Stack Overflow用户
提问于 2018-07-27 06:52:43
回答 1查看 32关注 0票数 0

我正在尝试为Rails应用程序设置与我的模型的关联,该应用程序将管理逃生室游戏。

基本上,有三种模型。

  • Player
  • Game大师
  • 游戏

玩家会要求玩游戏。然后,游戏管理员将在游戏开始之前批准每个请求。

我假设通过使用关联has_many:来关联模型,如下所示

这里有一个我认为应该如何设置关联的示例。

代码语言:javascript
复制
class GameMaster < ApplicationRecord
  has_many :games
  has_many :players, through: :games
end

class Game < ApplicationRecord
  belongs_to :game_master
  belongs_to :player
end

class Player < ApplicationRecord
  has_many :games
  has_many :game_masters, through: :games
end

如果我走对了路,请告诉我

EN

回答 1

Stack Overflow用户

发布于 2018-07-27 10:26:16

因为一个游戏可以有多个玩家,一个玩家可以有多个游戏,所以你需要一个在一个组合键中组合game_id和player_id的表关联。

通过这种方式,玩家可以在许多游戏中请求玩。

游戏大师同样需要,如果一个游戏可以有多个大师,可以批准玩家的请求。

PlayerGame将设置游戏的玩家,MasterGame将设置游戏的主控者:

因此,您必须具备:

代码语言:javascript
复制
class Player < ApplicationRecord
  has_many :player_games
  has_many :games, through: :player_games
end

class Master < ApplicationRecord
  has_many :master_games
  has_many :games, through: :master_games
end

class Game < ApplicationRecord
  has_many :player_games
  has_many :players, through: :player_games
  has_many :master_games
  has_many :masters, through: :master_games
end

class PlayerGame < ApplicationRecord
  belongs_to :player
  belongs_to :game
end

class MasterGame < ApplicationRecord
  belongs_to :master
  belongs_to :game
end

如果用户可以成为主用户,情况可能会有所不同。让我知道。建议,绘制一个易于理解的实体关系模型。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51548527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档