gpt4 book ai didi

ruby-on-rails - ActiveRecord::AssociationTypeMismatch:用户预期,得到 Fixnum

转载 作者:行者123 更新时间:2023-12-02 17:10:02 24 4
gpt4 key购买 nike

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

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

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

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

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

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

最佳答案

它只是说它需要一个 User 并且你给出一个 Fixnum

你应该做类似的事情

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

或者,如果您想通过 ids 为用户提供

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

关于ruby-on-rails - ActiveRecord::AssociationTypeMismatch:用户预期,得到 Fixnum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17345024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com