Good Day,
I have been using the book of Agile Web Development with Ruby on Rails 4 and I have been trying to use a Model Module in the directory: app/models/concerns/MyModule.rb
I found some documentation on how to do it for previous versions of rails using the /lib folder and some other methods but I need a simple method to call my Module inside my Model.
Module MyModule
extends ...
#
def some_method_in_MyModule
end
end
I have tried this:
app/models/users.eb
Class ...
include MyModule
a = some_method_in_MyModule
#
end
but rails keeps saying it is not correct.
How do I include a module method in a model with ruby on rails 4?
In previous versions of Rails, we needed to save files in the /lib folder, as we have the new folder app/models/concerns in Rails 4, we can directly call the method from the module in a model without any helper. e.g:
app/models/concerns/MyModule.rb
Module MyModule
extends .. #
def some_method_in_my_module
end
end
in our model we call it as:
class ...
#
@usuario = MyModule.some_method_in_my_module
#
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With