为什么在 rails 中引用灯具时 id 值会发生变化

Why does id value change when referring fixtures in rails

我有以下灯具。

users.yml

first_user:
  name: User1
  password_hash: <%= BCrypt::Engine.hash_secret('password', 'a$xKPXy2QabH6ThBjo7gNB8O') %>
  password_salt: a$xKPXy2QabH6ThBjo7gNB8O
  role: 1

stores.yml
one:
  name: MyFirstString
  user_id: <%= ActiveRecord::FixtureSet.identify(:first_user) %>

但是,当我加载 Store.first.user_id 时,我得到的值是 979462526,而 User.first.id980190962。我认为这些值不应该不同。请纠正我的理解或指出我的代码中的错误(如果有)。模型结构为:一个用户有多个店铺。

我认为ActiveRecord::FixtureSet.identify(:first_user)first_user夹具本身无关。
参见 source code

根据评论,它只是为给定的标签生成标识符。在你的例子中是 :first_user.

所以你可以打开 rails console 并手动完成

Zlib.crc32('first_user') % (2 ** 30 - 1) 
# => 979462526

你会得到同样的号码。
所以它与任何特定的固定装置无关(因为我没有任何固定装置)。

更新

也许你应该尝试使用关联而不是直接链接 ID。

# users.yml
first_user:
  name: User1
  ...

# stores.yml
one:
  name: MyFirstString
  user: first_user

这样我希望它能像预期的那样工作。