I have a Ticket
model with basic uniqueness validation on a string attribute ticket_number
validates :ticket_number, uniqueness: true
Here is the input code for the ticket_number
<%= f.number_field :ticket_number, :value => @ticket.ticket_number || (Ticket.exists? ? Ticket.maximum(:ticket_number).next) : 1 , :class => 'form-control' %>
The validation seems to work as expected, however when we deployed the app and probably more than one users were entering tickets
at the same time, which shouldn't cause an issue (right?) we found duplicated records (same ticket number) in the database(MySQL).
It only happened once in a total of 600+ tickets so far but why and how to prevent that from happening ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…