JohnMerlino
JohnMerlino

Reputation: 3928

Ensuring a unique number to identify something

I built a shopping cart. One of them is paypal standard which requires unique invoice integer. From the shopping cart, I just use the id of the current cart. However, the user can reactivate the service from within the web application itself (which is independent of the shopping cart site). This means I need a unique number and cannot rely on cart id, since the application itself has no cart. So I am thinking to take the current datetime in ruby and convert it to an integer:

DateTime.now.to_i 
=> 1370617672

DateTime.now.to_i
=> 1370617700

Will this guarantee a unique number always (since datetime is always changing). And if not, what other option would I have?

Upvotes: 0

Views: 534

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230551

If you need unique identifier, you should make such. For example, SecureRandom.uuid:

p SecureRandom.uuid #=> "2d931510-d99f-494a-8c67-87feb05e1594"

(I assume that PayPal accepts arbitrary strings and doesn't require actual integers)

Upvotes: 1

Related Questions