danielsmith1789
danielsmith1789

Reputation: 682

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

I am trying to save a filename with the same name as the hash of the corresponding record.

Currently the data importer is using rails, but the database and corresponding schema, were generated outside of the application.

When I read the Account model in psql I see:

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

when I use ActiveRecord I get the following: 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"

When I try to access the hash using Account.first.hash I get -1029718433662254257 instead of "5062a455-ad6e-4104-ae49-92d12b1fbd27".

I essentially want to do filename = "#{account.hash}.png" but now all of my file names are negative numbers instead of the UUIDs.

Any idea what is going on?

Upvotes: 2

Views: 368

Answers (1)

Eyeslandic
Eyeslandic

Reputation: 14900

hash is a method on the base Object in Ruby. Nothing you can do about that really* except change the name of the field in your db. 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.


As suggested in the comments you can also access it directly with

# in your view or model
account[:hash]

Upvotes: 2

Related Questions