应该a匹配器应该validate_uniqueness_of在范围内失败 [英] shoulda matchers should validate_uniqueness_of failing with scope
问题描述
我有 3 个模型,用户、游戏和玩家.用户和游戏之间基本上是多对多的关系,以玩家作为连接表,除了玩家有其他信息所以它有自己的模型.
玩家需要一个唯一的游戏 ID 和用户 ID 组合,所以我试图在玩家中说:
validates_uniqueness_of :user_id, :scope =>:game_id
然后在我的规范中,我说(使用 shoulda 匹配器):
它{应该validate_uniqueness_of(:user_id).scoped_to(:game_id)}
这里是玩家定义的关系:
belongs_to :game, :inverse_of =>:玩家归属于:用户,:inverse_of =>:玩家
但我在该规范上收到 ActiveRecord::statementinvalid 错误
ActiveRecord::StatementInvalid: Mysql2::Error: Column 'game_id' 不能为 null: INSERT INTO `players` ETC ...
知道出了什么问题吗?
这是一个 已知问题.如果范围字段依赖于模型并且还设置为 :null => ,则会出现此错误.错误
.
另外,请查看 rails 列不能为空:>
I have 3 models, user, game, and player. There's basically a many to many relationship between users and games, with players as the join table, except players have other info so it has its own model.
A player needs a unique combo of game id and user id, so I tried to say in player:
validates_uniqueness_of :user_id, :scope => :game_id
and then in my spec, I said (using shoulda matchers):
it { should validate_uniqueness_of(:user_id).scoped_to(:game_id)}
here are the relationships the player defines:
belongs_to :game, :inverse_of => :players
belongs_to :user, :inverse_of => :players
yet I'm getting an ActiveRecord::statementinvalid error on that spec
ActiveRecord::StatementInvalid: Mysql2::Error: Column 'game_id' cannot be null: INSERT INTO `players` ETC...
Any idea what's going wrong?
It is a known issue. It gives this error if the scoped field is dependent on the model and is also set to :null => false
.
Also, do have a look at rails Column cannot be null:
这篇关于应该a匹配器应该validate_uniqueness_of在范围内失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!