Ramkumar
Ramkumar

Reputation: 453

what is difference between using concerns vs modules in rails?

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.

Upvotes: 38

Views: 19989

Answers (3)

zinovyev
zinovyev

Reputation: 2163

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;

Upvotes: 22

huyhoang-vn
huyhoang-vn

Reputation: 335

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.

Upvotes: 3

Oleg Antonyan
Oleg Antonyan

Reputation: 3113

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.

Upvotes: 8

Related Questions