我真的是 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)?
谢谢!