I
I
Ivan Nesterovich2013-12-19 10:55:44
Ruby on Rails
Ivan Nesterovich, 2013-12-19 10:55:44

How to organize the generation of a unique 5-digit order number (numbers only) in ruby ​​on rails?

How to organize the generation of a unique 5-digit order number (numbers only) in ruby ​​on rails?
I want to organize an "Order Status" page... If you just specify an id, you can get a lot of unnecessary information (understand how many orders there are, view the statuses of other orders, etc.)

Answer the question

In order to leave comments, you need to log in

5 answer(s)
G
Gleb Erofeev, 2013-12-19
@vanderv

If you want a simple number (5 digits), give access to the order only to those who ordered it (after authorization). if the visitor is not authorized or did not order a specific order, he is always landed, for example, on the authorization page (or if he is authorized, there is no such order). This is not hard to do with before_filter in the controller.
I recommend that the number itself be formed according to a simple rule (so that it is easy for either the client or the client service to navigate it). For example, sew a date into it (the number of the week in the year and the number of the day in the week, for example, these are 3 digits. Another 2 for the order number on that day.).
The order number for the current day can be selected, for example, from an array of numbers from 0 to 99 and randomly selected and the selected number removed from the array. It is clear that if there are more than 100 orders per day, this algorithm will not allow you to generate 101 orders.

T
The Whiz, 2013-12-19
@modernstyle

I would generate a random number,
connect it with the order number.
You can add an order_string to the table, for example.
Visibility settings for other attributes can be given to Cancan. Something like this.

S
sdevalex, 2013-12-19
@sdevalex

> If you specify just id
ID, you can start not with 1, but with another large number. When creating a table, just specify a different next id.
> view the status of other orders A
5-digit number can be picked up very quickly if someone wants to see all the statuses. If you give the page to everyone who knows the ID, then just do not leave personal data there (names, addresses).

A
Andrey Polyakov, 2013-12-19
@magnitudo

Correctly they tell you about the selection of codes and size, 5 digits (99999) is too little.
But if you really want, then all sorts of CRCs (8,16,32) are very well suited - choose for every taste. You take some secret text, at the end, according to certain rules, add the real id and count the CRC. The advantage of CRC is that with minor changes in the input data, the output value changes greatly, i.e. it is very easy to detect serial brute force from one host and block it.
You can look in the direction of all sorts of hashes, but they have a large bit depth and they are considered more difficult.

S
Stanislav Skobelkin, 2013-12-19
@Slko

On my resource, I had to implement this, but only so that the result was a sequence of characters, like a YouTube video ID. And at the same time, so that from this sequence of characters you can get the original number. For these purposes, you can use any block cipher, which at the output gives a sequence with a length equal to the original. I don't know if there are ciphers with a 16 bit block so that the number is small enough. In my case, I used my bicycle based on the Feistel network. A very simple scheme to understand and implement.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question