问题
Just now I started to using Concerns in rails, but i have doubt why we go for concerns, because we can achieve same thing on module & mixing concept. So please any one tell about shat is the use of concerns instead of using module.
回答1:
It's well described here: http://api.rubyonrails.org/classes/ActiveSupport/Concern.html
In short:
Concerns allow you to use
#included
and#class_methods
instead ofself.included
hook with additional moduleClassMethods
creation;Concerns give you a better dependency resolution for modules included in each other;
回答2:
ActiveSupport::Concern
adds some convenient features (i.e class_methods
) to your module. You can use "pure" ruby modules without extending it. Essentially you create a module which you mix-in to a class. Doesn't matter if this module extends AS::Concern, the mechanism is the same.
回答3:
when you write in concern that mean you are making one module. My opinion is concern and module be similar together. Concern can appear somewhere as model, controller and at here you can write module for yourself. And with general module is write in lib folder. Both can be used by way include or extend into a class.
来源:https://stackoverflow.com/questions/34307469/what-is-difference-between-using-concerns-vs-modules-in-rails