← Back to blog
· 1 min read · Modules Rails

Include Class Methods and Validation Using Module in RubyOnRails

{% codeblock lang:ruby%} class User include TestModule end

module TestModule def testinstancemethod puts "Test Method" end

def self.included(base) base.extend ClassMethods end

module ClassMethods def myclassmethod puts "Class Methods" end end end {% endcodeblock %}

Here myclassmethod is Class Method.we can define more class methods in module ClassMethods

Validations and call backs are called without prefix 'self'. so for validations and call backs we have to explicitly set the class

{% codeblock lang:ruby%} module TestModule def self.included(base) base.validatespresenceof :bar base.beforesave :somemethod end end {% endcodeblock %}