3

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?

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.