34

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.

Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
Ramkumar
  • 413
  • 1
  • 4
  • 6

3 Answers3

18

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;

saudes
  • 202
  • 2
  • 15
zinovyev
  • 2,084
  • 1
  • 22
  • 32
8

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.

Oleg Antonyan
  • 2,943
  • 3
  • 28
  • 44
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.

huyhoang-vn
  • 335
  • 1
  • 3
  • 13