Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

312
Views
Loading module into User Model in Rails

Trying to make available the methods I have stored in a Module which is located in app/models (side note: not sure if this is the correct place for modules?).

Module:

module MyModule
    class MyClass
        def some_method
           # do something
        end
    end
end

User Model:

class User < ApplicationRecord
    
    include MyModule

    def another_method
       some_method
    end
end

I am getting a NoMethodError: NoMethodError (undefined method 'some_method' for #<User:0x00007f6a3ce452c0>

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You seem to have missunderstood what what modules and classes do in Ruby. In Ruby a module is simply an object that wraps a set of methods and constants.

A module can extend other modules, classes and objects and can be included in classes thus implementing multiple inheritance. Modules in Ruby fill the role that traits, namespaces and singletons do in other languages.

Classes are actually modules (Module is part of the ancestors chain of Class) with the key difference that you can make instances of a class and that class can inherit from a single other class and cannot extend other objects or be included.

The code example here actually doesn't make sense. If you want to declare a method that will be available to classes that include a module you want to declare it in the module itself:

module MyModule
  def some_method
    # do something
  end
end

When you then call User#another_method it will look in the ancestors chain of the User class until it finds the method which is defined in MyModule.

module MyModule
  class MyClass
    def some_method
      # do something
    end
  end
end

Will actually definte the class MyClass with an instance method that is only available to instances of MyClass. The only thing that the module does here is change the module nesting so that the class is defined in MyModule instead of the global namespace.

over 4 years ago · Santiago Trujillo Report

0

If you want to mix in a method from a method into your class then just put the methods directly in the module (without an intermediate class).

Module:

module MyModule
  def some_method
    # do something
  end
end

User Model:

class User < ApplicationRecord
  include MyModule

  def another_method
    some_method
  end
end
over 4 years ago · Santiago Trujillo Report

0

Have a look at this answer, you need to instantiate your Class first. Or if you want to

class User < ApplicationRecord
    
    include MyModule

    def another_method
       my_instance = MyClass.new
       my_instance.some_method
    end
end

As for a place to store your Module, have a look at this guide about service objects, it gave me some inspiration when it comes to different modules.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!