0

我是真的新的 Ruby on Rails 3和我正在一个简单的国际象棋游戏应用程序。我打算创建以下型号:RoR3模型协会(游戏belongs_to两名球员)

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:日期时间,的updated_at:日期时间。我也省略了'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),它是隐含的吗?

谢谢!

回答

3

给出的迁移上面你想设置你的游戏模型,如下所示:

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 

这将使用您的自定义外键,并让您每个关联链接为一个belongs_to的来电!

另外,如果你想轨“猜”的foreign_key设置,您就需要设置迁移就像这样:

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

如果你这样做,你仍然需要指定:CLASS_NAME =>'Player'选项用于每个belongs_to调用。

+0

我喜欢你的命名更好!尼斯。 –

+0

嗨科迪,我编辑的帖子也包括一个不同的迁移选项,对不起,忘了把它包括在我原来的文章:) – roboles

+0

现在,是否有可能做@ player.games?如何设置? –

0

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

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

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

不应该有改变的其他车型。

2

我在同一条船:新的Rails和建设一个象棋应用程序。我开始使用玩家和游戏之间的has_and_belongs_to_many关系,但我无法弄清楚如何正确地模拟白人和黑人玩家角色。

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

首先,我增加了第三个模式叫座位,然后设置我的播放器和游戏模式有一个的has_many:通过像关系:

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 

然后在游戏模式,我加了一些辅助方法,这样我可以得到白人球员与游戏.white_player并设置游戏。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 

我很想知道,以改善这方面的任何。