2

我正在使用初始数据填充 Rails 3.2 应用程序。我有一个 Country 和 State 模型。在我的状态模型中

  validates :country_id,
    presence: true

  validates :name,
    presence: true,
    uniqueness: {
      scope: :country_id
    }

当我尝试创建一个状态时,我得到了

State Exists (0.9ms)  SELECT 1 FROM "states" WHERE ("states"."name" = 'my-test-state' AND "states"."country_id" = 220) LIMIT 1
  Country Load (0.6ms)  SELECT "countries".* FROM "countries" WHERE "countries"."id" = 220 LIMIT 1
   (0.6ms)  ROLLBACK

问题是,State 模型中只有 4 条记录,没有一条与我试图创建的唯一一条相同。这绝对是独一无二的记录。

为什么验证会失败?我的语法对于验证两列是否正确?(姓名 + country_id)

谢谢

4

0 回答 0