简体   繁体   中英

Check for unique value before saving record

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM