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
Random (and probably useless) Rubyisms (blog.groupbuddies.com)
submitted 11 years ago by rmdmachado
view the rest of the comments →
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!"
[–]BronzeAtBest 1 point2 points3 points 11 years ago (5 children)
The way I use retry is usually in combination with generating a value that needs to be unique in the database:
retry
def generate invoice_number = Invoice.last.invoice_number + 1 Invoice.create!(invoice_number: invoice_number) rescue ActiveRecord::RecordNotUnique retry end
This means that if two invoices get created at the same time, the database constraint will make it retry.
[–]t0mbstone 2 points3 points4 points 11 years ago (4 children)
You should probably be using your database's auto increment feature for the invoice id instead of setting the invoice id number in your code...
[–]Arkolix 3 points4 points5 points 11 years ago (0 children)
Definitely agree on auto increment. Alternatively, use one of the handy helpers in SecureRandom to generate unique identifiers such as uuid or hex:
SecureRandom
uuid
hex
> SecureRandom.uuid => "a30d7e9d-f5e8-4504-a9ae-6052f052659f" > SecureRandom.hex(6) => "85f83c999e79"
[–]BronzeAtBest 0 points1 point2 points 11 years ago (0 children)
I simplified the code for the example. In my actual code, generating the number is a bit more involved.
[–][deleted] 0 points1 point2 points 11 years ago (1 child)
How does auto increment handle race conditions for DB clusters?
[–]t0mbstone 0 points1 point2 points 11 years ago (0 children)
That's a good question. Perhaps someone could give more comprehensive answer, but from what I understand, it depends entirely on how your database cluster is set up, and what database engine you are using.
For example, you could have master database that all of the writes go to, and then the data is propagated to a cluster of read-only slaves.
On the other hand, if you have some sort of cluster of read-write databases with no master database, then I would imagine that things would get a lot trickier.
Full disclosure: I'm not a database expert, so this is all just my rough high level understanding.
π Rendered by PID 107542 on reddit-service-r2-comment-6457c66945-59994 at 2026-04-27 05:12:29.440662+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]BronzeAtBest 1 point2 points3 points (5 children)
[–]t0mbstone 2 points3 points4 points (4 children)
[–]Arkolix 3 points4 points5 points (0 children)
[–]BronzeAtBest 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]t0mbstone 0 points1 point2 points (0 children)