what is difference between using concerns vs modules in rails?
Asked Answered
B

3

38

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.

Bourgeoisie answered 16/12, 2015 at 8:49 Comment(2)
Have a look at the DOCSHong
Dependency resolution, as mentioned in the docs.Workmanlike
V
22

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;

Variform answered 11/10, 2017 at 10:58 Comment(0)
H
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.

Hypnos answered 16/12, 2015 at 9:14 Comment(0)
P
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.

Pros answered 16/12, 2015 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.