ActiveRecord的:: AssociationTypeMismatch:用户期望,得到了Fixnum对象 [英] ActiveRecord::AssociationTypeMismatch: User expected, got Fixnum

查看:463
本文介绍了ActiveRecord的:: AssociationTypeMismatch:用户期望,得到了Fixnum对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么我得到以下错误:

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

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

当我这样做,在轨控制台: @game = Game.create(:玩家=> 1060:出场=> 1061)

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!

推荐答案

它只是说,它希望有一个用户,你给一个Fixnum对象

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

您应该做这样的事情

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

或者,如果你想通过IDS给用户

Or, if you want to give the users by ids

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

这篇关于ActiveRecord的:: AssociationTypeMismatch:用户期望,得到了Fixnum对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆