Rails Concerns vs. Modules and when to use each

  Рет қаралды 2,317

webcrunch

webcrunch

Күн бұрын

As a novice or professional Ruby on Rails developer, you're likely familiar with the concepts of Concerns and Modules. Both are used to organize and reuse code but have different purposes and distinct use cases.
In this post, I’ll tap into the differences between Rails Concerns and Modules, provide real code examples, and discuss best practices for when to use each.
What are Concerns?
Rails Concerns are a way to extract functionality from models, controllers, and other classes into reusable modules. They were introduced in Rails 4 as a replacement for the old "model concern" pattern. Concerns are typically used to share code between models, controllers, or other classes that have similar functionality.
What are Modules?
Modules are a way to organize namespace-related code. They can be used to group related classes, methods, and constants. Modules are often used to create a namespace for utility methods or to encapsulate more complex logic. If your actual models become huge, extracting to an independent module might be a good focus.
Read the full tutorial here: webcrunch.com/...
======
👋 I'm Andy Leverenz, a passionate product designer and developer. I love creating and sharing my knowledge through design, coding, and writing. Join me on my journey by checking out my blog, Web-Crunch (webcrunch.com), where I publish tutorials, articles, and the occasional vlog about design and development.
📰 Never miss an update! Click here to subscribe: www.youtube.co...
💻 Read the written version:
webcrunch.com/...
💎 New to Ruby on Rails? Enroll in my course HELLO RAILS:
hellorails.io
💻 The Blog (my source of truth): webcrunch.com
Additional Links:
🎨 Bring life to Rails projects with Rails UI: railsui.com.
👨‍💻 Hire me: Visit: railsui.com/cu...
🐤 / webcrunchblog .
⚙️ github.com/jus....
Also, this stuff takes a long time to make, but I love to do it. To help me keep at it, consider supporting me. If not monetarily, subscribe to the channel or share it with someone!
✨ github.com/spo...
☕️ www.buymeacoff...

Пікірлер: 13
@BubblegumSneaker
@BubblegumSneaker 8 ай бұрын
This helped me understand better something I already thought I understood.
@repoles
@repoles 8 ай бұрын
Thank you very much! I always watch your videos. They help me a lot!
@MarceloAlarcon-tx6qe
@MarceloAlarcon-tx6qe 8 ай бұрын
I think we can also use concerns in a controller to separate all methods that are not a direct action of the controller. For example a method to generate a cookie would go in the concern, while the action that uses it would obviously be in the controller. This is necessary because we can't use helpers in controllers. What do you think about this practice?
@haroldpepete
@haroldpepete 8 ай бұрын
i have personally used concern in poymorphic associations and when i want some special behavior in several model, and i use module to declaree functions that i need to use in my controllers or services, working with component in rails i make a metaproming module to declare instance variables witth a default values making a merge of all parameters the component can recieve
@IanBradbury
@IanBradbury 8 ай бұрын
Always super helpful videos. Thanks.
@Webcrunch
@Webcrunch 8 ай бұрын
Happy to help!
@ahmed_aboelleef
@ahmed_aboelleef 8 ай бұрын
thanks for sharing What is the editor theme name ?
@Webcrunch
@Webcrunch 8 ай бұрын
Spacegray 80s I believe
@formigarafa
@formigarafa 8 ай бұрын
You don't really need that include in the controller if you are using the "static" method calls. You can think of include method pretty much as defining the methods in the module in the context of the class where it is being included. This won't apply for `def self.xyz` methods though. You can operate with the `def self.methods` in similar form using the extend method but the result affects the class not the instances (as include would). This rabbit hole goes a bit deeper, it is meta-programming all the way down. It would become a book on this comment.
@eotfofiw2748
@eotfofiw2748 3 ай бұрын
I am new to rails and was about to ask... With no including, and no extending going on here... Wouldn't this just be a require? I guess the include brings it into scope even if it's not actually including instance methods.
@BillyWatkisonz
@BillyWatkisonz 10 күн бұрын
I subscribed when u misspelled presence
@abdullahsaleh7196
@abdullahsaleh7196 8 ай бұрын
keep going 🫡
10x your chances of landing a Ruby on Rails Job
12:47
webcrunch
Рет қаралды 1,9 М.
Understanding Active Record Associations
15:16
webcrunch
Рет қаралды 35 М.
UFC 310 : Рахмонов VS Мачадо Гэрри
05:00
Setanta Sports UFC
Рет қаралды 1,2 МЛН
Rails helpers: How to use them right
13:52
Alberto Almagro
Рет қаралды 1,1 М.
5 Secrets to Becoming a Badass Ruby on Rails Developer
28:01
Mix & Go
Рет қаралды 26 М.
Rails 8 + Claude: How I Built an Al App My Kids LOVE
6:15
Rails Quest by Kaleb Lape
Рет қаралды 1,2 М.
Modules vs Concerns
12:43
TypeFast
Рет қаралды 4,2 М.
RailsConf 2022 - Event Streaming on Rails by Brad Urani
31:26
Ruby Central
Рет қаралды 6 М.
RSpec spec_helper vs rails_helper Explained
20:27
Brett Codes
Рет қаралды 852
Ruby on Rails in 100 Seconds
3:33
Fireship
Рет қаралды 507 М.