You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A Rails Concern is a module that extends the ActiveSupport::Concern module. Concerns allow us to include modules with methods (both instance and class) and constants into a class so that the including class can use them.
A concern provides two blocks:
included
The code inside the included block is evaluated in the context of the including class.
You can write class macros (validations, associations, scopes, etc.) here, and any methods become instance methods of the including class.
class_methods
The methods added inside this block become the class methods on the including class.
Instead of the class_methods block, you can create a nested module named ClassMethods.
To use this concern, you include the module as usual. For example, if the Post model wanted the visibility functionality, it would include the Taggable concern.
Justin Weiss has a great article that describes just how you can use Rails concern to refactor feature filter Product by attribute.
A Rails Concern is a module that extends the ActiveSupport::Concern module. Concerns allow us to include modules with methods (both instance and class) and constants into a class so that the including class can use them.
A concern provides two blocks:
included
class_methods
class_methods
block, you can create a nested module namedClassMethods
.For example, a concern named
Taggable
To use this concern, you include the module as usual. For example, if the
Post
model wanted the visibility functionality, it would include theTaggable
concern.Justin Weiss has a great article that describes just how you can use Rails concern to refactor feature filter
Product
by attribute.In
Product
model:In controller that filter products by each attributes:
The text was updated successfully, but these errors were encountered: