what is difference between using concerns vs modules in rails?

爱⌒轻易说出口 提交于 2019-12-08 15:48:27

问题


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 of self.included hook with additional module ClassMethods 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!