validates_uniqueness_of on a facade column

You may have to roll your own like:

def before_save
    find(:all, :conditions => ['ip_address = ?', convert_to_int(self.ip_address)]).empty?

Spike Grobstein wrote:

just a note, convert_to_int is simply a facade that you should replace with whatever method you are using to do the conversion, I assume IPAddr#to_i, but none-the-less, you get the gist of it.

William Pratt wrote:

yep, or you could clean it up even more by eliminating the unnecessary instance variable:

def validate
# check if the ip_address is unique
unless Machine.find(:all, :conditions => ['ip_address = ? and id <> ?', IPAddress.to_int(self.ip_address),]).empty?
  errors.add :ip_address, "is already taken"

However, both will work, this one just uses a tad less memory. Glad it worked for ya.

Spike Grobstein wrote: