0
class Person
  validates :full_name, presence: true
  validates :display_name, unique: ???
  attr_acessible :full_name, :display_name
end

Full names can not be unique, but the display name must be unique. If I want to have the full_name as a fallback, but I still want to check for uniqueness of the display_name.

What is the cleanest way to validate the uniqueness of coalesce(display_name, full_name) or whatever is necessary to achieve the same effect?

michelpm
  • 1,795
  • 2
  • 18
  • 31
  • In case someone doesn't know what I mean by `COALESCE`, it is the same as `NULLIF` on MSSQL and `NVL` on Oracle, or operators like `??` in C#, `?` in CoffeeScript or a simple `or`/`||` in most dynamically typed languages like JavaScript and Ruby. – michelpm Nov 10 '12 at 00:25

1 Answers1

-1

This may be what you are after if not please explain the coalesce further validates :display_name, uniqueness => true

Update

after additional info in comment

validates :display_name, :uniqueness => {:scope => :full_name}

If this is not equivalent to your requirements then

validates :coalesce

def coalesce
  #put your validation in the method here i.e
  if self.display_name == false
    #then check full_name etc.
  end
end
Jesse Whitham
  • 824
  • 9
  • 32