0

I have a class called User, that needs to include a module. The class looks like this:

require 'sequel'
require 'modules/validations'

class User < Sequel(..)
  many_to_one :country
  includes ::Validations

  validates_email(:email)
end

The module is defined in a subfolder called modules. It has been added to the $LOAD_PATH and Ruby is no complaining about the loading. The module looks like this:

module Validations
   def validates_email(attr, options = {})
       email = super.email
   end
end

The error I am getting is : undefined method 'validates_email' for User:class

What am I missing to make this work properly?

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
codingbunny
  • 3,989
  • 6
  • 29
  • 54

1 Answers1

0

If you want to use modules to define class methods you should use the extends method.

class User < Sequel(..)
  extend ::Validations
  validates_email(:email)
  ...
end

I suggest reading this article: http://railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/

gmalette
  • 2,439
  • 17
  • 26