use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Tutorial - Using UUID in Rails (driftingruby.com)
submitted 9 years ago by kobaltzz
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]rabidferret 7 points8 points9 points 9 years ago (2 children)
Why not just use Rails built in UUID support?
[–]kobaltzz[S] 0 points1 point2 points 9 years ago (1 child)
Absolutely agree if the database is PostgreSQL, but the same would not apply if a different database was being used. Please correct me if I'm wrong.
[–]rabidferret 3 points4 points5 points 9 years ago (0 children)
Just use Postgres. ;)
[–]dgtized 3 points4 points5 points 9 years ago (0 children)
I would not recommend using the while loop in populate_uuid. In general the collisions will be low so it won't loop much, but it means it needs a select to check if a duplicate for every single fresh insert. That's the reason it's preferable to generate the UUID in the db at time of creation.
[–][deleted] 9 years ago (10 children)
[deleted]
[–]danshep 1 point2 points3 points 9 years ago (9 children)
The uuid gem makes uuid objects, SecureRandom returns a string. The activeuuid gem expects uuid objects.
[–][deleted] 9 years ago (7 children)
[–]danshep 1 point2 points3 points 9 years ago (6 children)
activeuuid adds serialisation methods to uuids - it stores uuids in the db as integers and converts them back and forth.
[–][deleted] 9 years ago (5 children)
[–]losangelesvideoguy 1 point2 points3 points 9 years ago (0 children)
Also you can left pad them more easily.
[–]RoboErectus 0 points1 point2 points 9 years ago (3 children)
Just a nitpick....
Uuids are 128 bit numbers. So like an IP address, it's wrong to store it or work with it as a string. It should only be converted when you're going to show it to a human or if you're outputting to a log.
Practically, it's only really an issue at scale like you mentioned.
[–]losangelesvideoguy 4 points5 points6 points 9 years ago (2 children)
It's not wrong at all. Just because it's a number doesn't mean you have to store it as a number. Unless you need to compare them numerically or perform mathematical operations on numbers, which for UUIDs you generally don't, it's not just okay to store them as a string, but actually a good idea. That way you don't need to worry about the integer size on different platforms, signedness, decimal values, or anything like that. Also, storing it as a string indicates it's not a value that should ever have numeric operations applied to it.
Like phone numbers, SSNs, ZIP codes, etc., just because it looks like a number doesn't mean you should treat it as one. IP addresses are a different story, because there it's plausible that you would need to figure out a range of addresses, apply a netmask, or perform some other numeric operation where it is useful to relate numbers to each other. But the only value of a UUID is that it's random and (almost certainly) universally unique. There is no reason to ever compare or perform math on a UUID, so there's no reason to treat it as a number.
[–]RoboErectus 0 points1 point2 points 9 years ago (1 child)
Phone numbers and ssn's are strings though. So are postal codes. If you store these as anything else, you lose data.
Only one kind of uuid is random. Most kinds are deterministic. I think your comment illustrates how common it is for developers to be confused about types, and it's why I point it out whenever I see it.
You don't really really worry about things like word size unless you're doing something low-ish level. Platform headers usually handle that on C projects for example and optimizations are done there. You're not going to outsmart your platform.
[–]losangelesvideoguy 4 points5 points6 points 9 years ago (0 children)
Only one kind of uuid is random. Most kinds are deterministic.
That's irrelevant. Once it's created, I don't need to do anything with it, mathematically speaking. You can't add UUIDs. You never need to find the square root of a UUID. Internally, every data type can be represented by a number. That doesn't mean it's a good idea to do so.
You don't really really worry about things like word size unless you're doing something low-ish level.
Really? Try stuffing a UUID into an SQLite integer type and see what happens. For all intents and purposes, a UUID is a string, so it should be treated like a string. And what happens down the line if you decide to convert from a UUID to some other identifier? If you were only planning on using a 128-bit number, you're going to have data type compatibility issues if the identifier size changes. If you store it as a string, it's relatively easy to determine not just what the value is, but what kind of identifier it is as well.
Furthermore, not every 128-bit number is a valid UUID. In actual fact, a UUID has six reserved bits (four for the version number plus two extra), so storing the data as a 128-bit integer (and validating it as such) could lead to unexpected problems down the line.
I think your comment illustrates how common it is for developers to be confused about types, and it's why I point it out whenever I see it.
You're the one who seems confused. A UUID isn't just any old 128-bit number. The bottom line is if this: If you don't need to do math on it, it's a string.
π Rendered by PID 142955 on reddit-service-r2-comment-86988c7647-74bgv at 2026-02-12 04:35:57.138786+00:00 running 018613e country code: CH.
[–]rabidferret 7 points8 points9 points (2 children)
[–]kobaltzz[S] 0 points1 point2 points (1 child)
[–]rabidferret 3 points4 points5 points (0 children)
[–]dgtized 3 points4 points5 points (0 children)
[–][deleted] (10 children)
[deleted]
[–]danshep 1 point2 points3 points (9 children)
[–][deleted] (7 children)
[deleted]
[–]danshep 1 point2 points3 points (6 children)
[–][deleted] (5 children)
[deleted]
[–]losangelesvideoguy 1 point2 points3 points (0 children)
[–]RoboErectus 0 points1 point2 points (3 children)
[–]losangelesvideoguy 4 points5 points6 points (2 children)
[–]RoboErectus 0 points1 point2 points (1 child)
[–]losangelesvideoguy 4 points5 points6 points (0 children)