【发布时间】:2015-07-03 17:50:08
【问题描述】:
我正在为一个有很多玩家的游戏建模。玩家还可以玩多个游戏。我有用户、游戏和作为连接表的played_game。
这是我第一次尝试 has_many:通过关系,所以我希望这是一个简单的问题。或者至少是一个具有简单解决方案的问题。
class User < ActiveRecord::Base
has_many :played_games
has_many :games, :through => :played_games
end
class Game < ActiveRecord::Base
has_many :played_games
has_many :users, :through => :played_games
end
class PlayedGame < ActiveRecord::Base
belongs_to :users
belongs_to :games
end
当我尝试(从控制台)向用户添加游戏时会发生这种情况:
User.first.played_games << Game.first
结果:
ActiveRecord::AssociationTypeMismatch: PlayedGame(#70279902258580) expected, got Game(#70279901145600)
好吧,也许我误会了。也许我应该尝试添加games:
User.first.games << Game.first.id
结果:
NameError: uninitialized constant User::Games
感谢任何帮助或文档链接。提前致谢!
【问题讨论】:
标签: ruby-on-rails ruby activerecord has-many-through has-many