How to get md5 from a record and check if it is unique?

Asked

Viewed 44 times

0

I have an atypical need where several records are imported by lot, these records have no unique identifiers, each record has information about a debt.

I am imagining creating an md5 of each record so there is no duplicity. Of course I would remove the date fields as created_at and updated_at.

I thought I’d use the Digest::MD5.hexdigest where would create a column md5 and before saving would check if there is an identical record.

I want to know if anyone here has ever had a similar challenge and possible solutions to this challenge. Thank you.

1 answer

0


Are you persisting this data in a database? Why not let the task of knowing if it is unique to the database? You can put a single key in the column you want to force to be the index. Even make it her primary key (PK) instead of an entire ID. In fact Rails usually give a little more work if a PK is not defined.

In case you use MD5, I think it is possible but I see many solutions using UUID. Postgresql (if you’re using it) comes with build-in solutions to generate both MD5 and UUID.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.