Justin Ko wrote:
The ID number will mainly be used to keep track of the person’s
attendance. The person will check in via USB numeric keypad and if the
5 digit numbers are too close or similiar there is more room for error
(checking in wrong person because numbers are so close). I figured if
the numbers are random they will be spread out more from each other and
hence less room for error. The number of people will most likely be
To make each ID number unique I could use a validation for uniqueness.
If there is a better way to do all of this please let me know. Thanks!
rand generates a random number between 0 and 1. You can give an integer
argument to rand to define the upper limit of the numbers.
rand(1000) #=> 297
So you can add this to your model’s initialize method. This will make
sure that every new object has a random id.
class Foo < ActiveRecord::Base
self[:code] = rand(1000)
This doesn’t handle the unique problem however.