ActiveRecord :: AssociationTypeMismatch:用户期望,得到了Fixnum

时间:2013-06-27 13:50:15

标签: ruby-on-rails activerecord ruby-on-rails-3.2

我不明白为什么会出现以下错误:

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

当我在rails控制台中执行此操作时:@game = Game.create(:player => 1060, :played => 1061)

我只想在下面创建一个关于模型关联的新Game

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

如果有人有想法...... 非常感谢!

1 个答案:

答案 0 :(得分:20)

它只是说它想要一个用户而你给一个Fixnum

您应该执行类似

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

或者,如果您想通过ID给用户

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