Modules have 2 main goals:
Namespacing is typically used to streamline code more efficiently and provide more secure and consistent coverage.
But modules find their main application as mixins. Its a Ruby way to provide multiple inheritance. For example, let's say you have methods that you need to access through classes (for example, for different models / controllers, etc.). Instead of repeating these methods in each class, which do not necessarily apply only to this class, you abstract these methods into a module and include or extend the module into the corresponding class.
It depends on how tightly the module is connected to the application directory to decide how to store the module. Several templates when storing modules:
- The / lib directory, if the module does not particularly "interact" with the / application.
- Application / model catalog, but can lead to confusion with actual ActiveRecord models.
- 37 Signals entered a pattern , treating them as βproblemsβ and storing them in the application / problems.
However, if you have a method that you use only for your User Controller, it is recommended to put this code in the user model, because this should be the business logic for "User".
By 'ActiveRecords', I assume that you mean model classes (e.g. User). You can access model classes and perform ActiveRecord operations (such as User.find (: all)) on them in modules.
However, as you already guessed, you cannot use params, you will have to pass this as an argument to the modular method.
source share