Activerecord 将 UUID 读取为整数。我需要实际的 UUID 作为字符串

Activerecord reads UUID as an Integer. I need the actual UUID as a string

我正在尝试保存一个与相应记录的哈希同名的文件名。

目前数据导入器正在使用 rails,但数据库和相应的架构是在应用程序外部生成的。

当我阅读 psql 中的 Account 模型时,我看到:

select * from accounts where id=1

id                     | 1
first_name             | JOHN
middle_name            |
last_name              | DOE
suffix                 |
dob                    | 1985-11-29 00:00:00
gender                 | M             
hash                   | 5062a455-ad6e-4104-ae49-92d12b1fbd27

当我使用 ActiveRecord 时,我得到以下信息: Account.first

id: 1,
first_name: "JOHN",
middle_name: "",
last_name: "DOE",
dob: Fri, 29 Nov 1985 00:00:00 UTC +00:00,
gender: "M",
hash: "5062a455-ad6e-4104-ae49-92d12b1fbd27"

当我尝试使用 Account.first.hash 访问散列时,我得到 -1029718433662254257 而不是 "5062a455-ad6e-4104-ae49-92d12b1fbd27"

我基本上想做 filename = "#{account.hash}.png" 但现在我所有的文件名都是负数而不是 UUID。

知道发生了什么事吗?

hash 是在Ruby 中基于Object 的方法。除了更改数据库中字段的名称外,您对此无能为力*。 https://apidock.com/ruby/Object/hash

Generates a Fixnum hash value for this object. This function must have the property that a.eql?(b) implies a.hash == b.hash. The hash value is used by class Hash. Any hash value that exceeds the capacity of a Fixnum will be truncated before being used.


正如评论中所建议的,您也可以直接使用

访问它
# in your view or model
account[:hash]