[英]RoR3 Model associations (Game belongs_to two Players)
I'm really new to Ruby on Rails 3 and I'm working on a simple chess game application. 我真的是Ruby on Rails 3的新手 ,并且正在开发一个简单的国际象棋游戏应用程序。 I plan to create the following models: 我计划创建以下模型:
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
Assume they all have: id:int:primary_key, created_at:datetime, updated_at:datetime. 假设它们都有:id:int:primary_key,created_at:datetime,updated_at:datetime。 I also omitted fields like 'password_hash' and others. 我还省略了“ password_hash”等字段。 My issue is with associations, not so much with what fields I need to make the app work. 我的问题是关联,而不是我需要什么才能使应用正常工作。
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
Questions: 问题:
1) I want to link a Game to two Players, how can I specify that relationship? 1)我想将一个游戏链接到两个玩家,如何指定这种关系?
2) Do I have to specify things like game_id:int in 'rails generate model' or is it implicit when I do the relationships (belongs_to :player, has_many :games)? 2)我是否必须在“ rails generate model”中指定像game_id:int之类的东西,还是在进行关系时(belongs_to:player,has_many:games)隐式?
Thanks! 谢谢!
given the migration above you would want to set your Game model up as follows: 鉴于上述迁移,您需要按以下步骤设置游戏模型:
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
This will use your custom foreign keys, and enable you to link each association as a single belongs_to call! 这将使用您的自定义外键,并使您可以将每个关联作为一个单独的Emirates_to调用来链接!
Alternatively, if you would like rails to 'guess' the foreign_key setting you would need to set up your migration like so: 另外,如果您希望Rails“猜测” foreign_key设置,则需要像下面这样设置迁移:
rails g model Game white_player_id:integer black_player_id:integer title:string
If you do this, you would still need to specify the :class_name => 'Player' option for each of the belongs_to calls. 如果这样做,您仍然需要为每个属植物调用指定:class_name =>'Player'选项。
I'm in the same boat: new to Rails and building a chess app. 我在同一条船上:Rails的新手,并构建了一个国际象棋应用程序。 I started out using a has_and_belongs_to_many relationship between Players and Games, but I couldn't figure out how to properly model the white and black player roles this way. 我开始使用玩家与游戏之间的has_and_belongs_to_many关系,但是我不知道如何用这种方式正确地为白人和黑人玩家角色建模。
I ended up using a different approach than what roboles suggested because I needed a way to follow the relationship from Player to Games. 我最终使用了与roboles建议的方法不同的方法,因为我需要一种跟踪从玩家到游戏的关系的方法。
First I added a third model called Seats, then setup my Player and Game models to have a has_many :through relationship like: 首先,我添加了第三个名为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
This sets up the model so that game.players and player.games work. 这将建立模型,以便game.players和player.games起作用。
To track the white and black player, I added a column for color in the seats table: 为了跟踪白人和黑人球员,我在座位表中添加了颜色列:
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
Then in the game model I added some helper methods so that I can get the white player with game.white_player and set with game.white_player = foo 然后,在游戏模型中,我添加了一些辅助方法,以便可以通过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
I'm not sure if this is the best approach, but it seems to meet my requirements: 我不确定这是否是最好的方法,但似乎符合我的要求:
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
I'd be interested to know any ways to improve on this. 我很想知道任何改进的方法。
In your Game class add 2 fields like: 在您的游戏类中,添加2个字段,例如:
belongs_to :player_one, :class_name => "Player"
belongs_to :player_two, :class_name => "Player"
So this implies you have 2 integer fields in your DB player_one int, player_two int
因此,这意味着您的数据库中有2个整数字段player_one int, player_two int
The other models shouldnt have to change. 其他模型不必更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.