Reputation: 8494
My setup: Rails 3.0.9, Ruby 1.9.2
I want to generate a unique random number for a column right before creating a new record. What is the most efficient way to do this in Ruby? I understand that I can use validates_uniqueness_of but I want to ensure uniqueness beforehand.
Upvotes: 0
Views: 3750
Reputation: 2025
How about a before_create or before_save AR callback?
Something like:
class ModelNameHere < ActiveRecord::Base
before_create :generate_unique_number
private
def generate_unique_number
begin
self.number = unique_number_generator()
end while (not ModelNameHere.find_by_number(self.number).blank?)
end
The begin..end while is basically a do-while loop taken from this answer. Haven't tested though but I think it should work.
Upvotes: 4
Reputation: 153
maybe something like... self is new object
Object.currentkeys.each do currentkey
if self.key == current.key
#The key is allready in use
else
#You have unique key
end
end
Upvotes: 0
Reputation: 2554
If you want to absolutely ensure uniqueness in Ruby before saving the record, then wrap it in a transaction and lock the tables. Be aware that this can cause a significant impact on your performance.
self.class.transaction do
connection.execute('LOCK TABLES table_name WRITE')
# do your random generation until uniqueness validation passes here
connection.execute('UNLOCK TABLES')
end
Personally, I think I'd rather place a unique constraint on the column in the database and catch the resulting exception in case you encounter a real race condition.
Upvotes: 1