I have a couple of methods I’d like to share between models rather than copying and pasting them. In a controller, I can do this by putting the methods in application_controller.rb which automatically gets included in every controller.
Is there a similar thing for models?
Thanks!
You can create a file called
functionality.rbor something similar in thelibdirectory of your Rails app. Make the file named after the module to autoload it when Rails starts. For example, if I wanted to add flagging to multiple models, I’d create a file calledlib/flagging.rb, and it would look like this:In every model I wanted to add this functionality to, I’d do the following:
I’d then be able to do something like: