How to validate uniqueness only if value exists

I’m looking for the best way to use validation to confirm that an
optional field (hostname) is unique only if a value is entered in the
form.

I was originally following the path shown in Agile Web D. with
Rails first edition, page 66, to define the “validate” method, but
didn’t have a clue how to include a uniqueness test there. Right now, I
have the following and it seems to be working as intended:

validates_uniqueness_of :hostname, :if => :hostname?

I guess I’m looking for some validation myself that this is a good way
to handle this. I’d also love to hear of other ways to make this work.

Thanks.

-Mason

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs