Saurabh Shah Saurabh Shah - 4 months ago 22
Ruby Question

Equivalent of `exclusion` in sequel

What is the Sequel equivalent of "exclusion"?

For a simple example, in Active Record:

validates :subdomain, exclusion: { in: %w(www us ca jp),
message: "%{value} is reserved." }


How can this be written in Sequel?

Answer

Sequel doesn't provide the exclusion helper by default, but you could customize it yourself.

class Host < Sequel::Model
  def validate
    super
    errors.add(:subdomain, "#{subdomain} is reserved.") if %w(www us ca jp).include?(subdomain)
  end
end

See "Custom Validations" for more information.