I mean, I want to create a class method in my module that will be used by the class who includes the module. They are in separated files.
By far I have something like this:
module Base
def self.all
puts "All Users"
end
end
class User
include Base
end
But I’m getting: NoMethodError: undefined methodall’ for User:Class`
Can you please explain the problem and if what I’m doing is a bad practice or going against any principle?
You can
extendthe module in your class, your code should be like this:When you do something like this:
You’re actually adding the method in the module itself, you could call the previous method like this:
There is a way to just include the
moduleand get the behaviour that you want, but I don’t think this could be considered the “way to go”. Look at the example:But like I said, if you can go with the
extendclass method, it is a better suit.