Topic: Create Sudo Unique Value

is there a way where i can create a sudo unuque value for a field.

What i am trying to do is create a field that will only have unique values or NIL in it

i know that "validates_uniqueness_of" will not do it as there cant be 2 NIL's

Thanks

controller

 def update
    sudo_unique
    if account.update_attributes(params[:account])
      flash[:success] = t(:account_updated)
      redirect_to accounts_path
    else
      render 'edit'
    end
  end

private
  def sudo_unique
    unless params[:account][:sudo_unique_field] == NIL
      else
       check_account = Account.search(params[:account][:sudo_unique_field])
       if (a.last.id != account.id)
        flash[:success] = t(:sudo_unique_field_exist)
        return false
       end
    end
  end

Last edited by moiseszaragoza (2012-08-13 10:25:14)

Re: Create Sudo Unique Value

Firstly you need to change

unless params[:account][:sudo_unique_field] == NIL

to

unless params[:account][:sudo_unique_field].blank? #use .empty? for an array

This will take care of white space values
You can use secure random module to create unique data
Check out
http://rubydoc.info/stdlib/securerandom … cureRandom
and a blog on how to use it
http://www.themomorohoax.com/2009/09/22 … generators

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)