ActiveRecord::AssociationTypeMismatch: User expected, got Fixnum

不问归期 提交于 2019-12-18 22:25:50

问题


I don't understand why I get the following error:

ActiveRecord::AssociationTypeMismatch: User(#29943560) expected, got Fixnum

when I do that in rails console: @game = Game.create(:player => 1060, :played => 1061)

I just want to create a new Game regarding model associations below.

class User < ActiveRecord::Base
    has_many :game_as_player, :class_name => 'Game', :foreign_key => 'player_id'
    has_many :game_as_played, :class_name => 'Game', :foreign_key => 'played_id'
end

class Game < ActiveRecord::Base
    belongs_to :player, :class_name => 'User'
    belongs_to :played, :class_name => 'User'

    attr_accessible :player, :played, :score, :details, :viewed, :read
end

If anyone has an idea... Thanks a lot!


回答1:


It just says that it want a User and you give a Fixnum

You should do something like

@game = Game.create(:player => Player.find(1060), :played => Player.find(1061))

Or, if you want to give the users by ids

@game = Game.create(:player_id => 1060, :played_id => 1061)


来源:https://stackoverflow.com/questions/17345024/activerecordassociationtypemismatch-user-expected-got-fixnum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!