0

In ohm when I try to save a unique attribute it raises an error if the attribute is not unique. Is there a way to have it simply return false instead of raising the error?

This is the type of code I want, but it raises an error if the domain is not unique, so it never reaches the else clause:

domain = Domain.new(:domain => root_domain)
if domain.save
    return domain
else
    return Domain.with(:domain => root_domain)
end
Josh
  • 655
  • 5
  • 17

1 Answers1

0

Just use ruby rescue:

begin
  domain.save
  domain
rescue Ohm::UniqueIndexViolation
  return false
end
Yaro
  • 570
  • 3
  • 20