Topic: Nice way to generate a random and unique string?
I have a table (items) that has a field 'token' that is a randomly generated string which must be unique within the table. Currently i'm doing this:
token = random_string(20) #uses a helper
while Item.find_by_token(token) != nil
token = random_string(20)
end@item.update(:token => token)
But this feels a bit nasty. This must be a common problem, does anyone know a nicer way to do it?
thanks
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ i-williams