ruby-on-railsrubysequelsequel-gem

Sequel validations in concerns


I have a Sequel model like this:

class User < Sequel::Model
  include Notificatable

  def validate
    super
    validates_presence [:email]
  end
end

# concerns/notificatable.rb
module Notificatable
    extend ActiveSupport::Concern

    included do
      def validate
        super
        validates_presence [:phone]
      end
    end
end

And here I got a problem: Notificatable validate method overrides the same method in the User model. So there is no :name validations.

How can I fix it? Thanks!


Solution

  • Why use a concern? Simple ruby module inclusion works for what you want:

    class User < Sequel::Model
      include Notificatable
    
      def validate
        super
        validates_presence [:email]
      end
    end
    
    # concerns/notificatable.rb
    module Notificatable
      def validate
        super
        validates_presence [:phone]
      end
    end