繁体   English   中英

RoR3模型关联(游戏归属到两个玩家)

[英]RoR3 Model associations (Game belongs_to two Players)

真的是Ruby on Rails 3的新手 ,并且正在开发一个简单的国际象棋游戏应用程序。 我计划创建以下模型:

rails g model Player name:string
rails g model Game player_id_white:int player_id_black:int title:string
rails g model Comment player_id:int game_id:int comment_data:text
rails g model Move game_id:int player_id:int move_data:string

假设它们都有:id:int:primary_key,created_at:datetime,updated_at:datetime。 我还省略了“ password_hash”等字段。 我的问题是关联,而不是我需要什么才能使应用正常工作。

class Player < ActiveRecord::Base
  has_many :games #some have player_id_black, others as player_id_white
  has_many :comments
  has_many :moves
end

class Game < ActiveRecord::Base
  has_many :comments
  has_many: moves
  **belongs_to :player1??**
  **belongs_to :player2??**
end

class Comment < ActiveRecord::Base
  belongs_to :player
  belongs_to :game
end

class Move < ActiveRecord::Base
  belongs_to :player
  belongs_to :game
end

问题:

1)我想将一个游戏链接到两个玩家,如何指定这种关系?
2)我是否必须在“ rails generate model”中指定像game_id:int之类的东西,还是在进行关系时(belongs_to:player,has_many:games)隐式?

谢谢!

鉴于上述迁移,您需要按以下步骤设置游戏模型:

class Game < ActiveRecord::Base
  has_many :comments
  has_many :moves
  belongs_to :white_player, :class_name => 'Player', :foreign_key => 'player_id_white'
  belongs_to :black_player, :class_name => 'Player', :foreign_key => 'player_id_black'
end

这将使用您的自定义外键,并使您可以将每个关联作为一个单独的Emirates_to调用来链接!

另外,如果您希望Rails“猜测” foreign_key设置,则需要像下面这样设置迁移:

rails g model Game white_player_id:integer black_player_id:integer title:string

如果这样做,您仍然需要为每个属植物调用指定:class_name =>'Player'选项。

我在同一条船上:Rails的新手,并构建了一个国际象棋应用程序。 我开始使用玩家与游戏之间的has_and_belongs_to_many关系,但是我不知道如何用这种方式正确地为白人和黑人玩家角色建模。

我最终使用了与roboles建议的方法不同的方法,因为我需要一种跟踪从玩家到游戏的关系的方法。

首先,我添加了第三个名为Seats的模型,然后将Player和Game模型设置为具有has_many:through关系,例如:

class Game < ActiveRecord::Base
  has_many :seats
  has_many :players, :through => :seats
end

class Player < ActiveRecord::Base
  has_many :seats
  has_many :games, :through => :seats
end

class Seat < ActiveRecord::Base
  belongs_to :game
  belongs_to :player
end

这将建立模型,以便game.players和player.games起作用。

为了跟踪白人和黑人球员,我在座位表中添加了颜色列:

create_table "seats", :force => true do |t|
  t.integer  "player_id"
  t.integer  "game_id"
  t.integer  "color"
  t.datetime "created_at", :null => false
end

然后,在游戏模型中,我添加了一些辅助方法,以便可以通过game.white_player获取白人玩家并通过game.white_player = foo进行设置

class Game < ActiveRecord::Base
...
def white_player
  Player.joins(:games).where(:seats => {:color => 0, :game_id => self.id}).first
end

def white_player=(player)
  self.players << player

  s = self.seats.find_by_player_id(player)
  s.color = 0
  s.save
end

我不确定这是否是最好的方法,但似乎符合我的要求:

game.players # returns game's players
player.games # returns player's games
game.white_player # returns the white player
game.white_player = player # sets the white player

我很想知道任何改进的方法。

在您的游戏类中,添加2个字段,例如:

belongs_to :player_one, :class_name => "Player"
belongs_to :player_two, :class_name => "Player"

因此,这意味着您的数据库中有2个整数字段player_one int, player_two int

其他模型不必更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM