Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include a module method in a model with ruby on rails 4?

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?

like image 615
They_Call_Me_Joe Avatar asked Nov 25 '25 03:11

They_Call_Me_Joe


1 Answers

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
like image 127
They_Call_Me_Joe Avatar answered Nov 28 '25 16:11

They_Call_Me_Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!