ruby on rails - ActiveRecord::AssociationTypeMismatch: for has_many :through relationship -
i modeling game has many players. players can play multiple games. have users, games , played_game acting join table.
this first foray has_many :through relationships i'm hopeful simple question. or @ least question simple resolution.
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 this happens when try add game user (from console):
user.first.played_games << game.first results in:
activerecord::associationtypemismatch: playedgame(#70279902258580) expected, got game(#70279901145600) ok, maybe i'm misunderstanding. perhaps it's games should try add to:
user.first.games << game.first.id results in:
nameerror: uninitialized constant user::games any or link documentation appreciated. in advance!
the problem appears you've defined belongs_to associations on playedgame incorrectly plural when should singular. change them to:
class playedgame < activerecord::base belongs_to :user belongs_to :game end and should able use:
user.first.games << game.first
Comments
Post a Comment