Rails Concerns vs. Modules and when to use each

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/posts/rails-con...
======
👋 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: kzread.info...
💻 Read the written version:
webcrunch.com/posts/
💎 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/custom
🐤 / webcrunchblog .
⚙️ github.com/justalever.
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/sponsors/justalever
☕️ www.buymeacoffee.com/webcrunch

Пікірлер: 11

  • @repoles
    @repoles17 күн бұрын

    Thank you very much! I always watch your videos. They help me a lot!

  • @BubblegumSneaker
    @BubblegumSneaker18 күн бұрын

    This helped me understand better something I already thought I understood.

  • @IanBradbury
    @IanBradbury18 күн бұрын

    Always super helpful videos. Thanks.

  • @Webcrunch

    @Webcrunch

    17 күн бұрын

    Happy to help!

  • @haroldpepete
    @haroldpepete18 күн бұрын

    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

  • @MarceloAlarcon-tx6qe
    @MarceloAlarcon-tx6qe17 күн бұрын

    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?

  • @formigarafa
    @formigarafa17 күн бұрын

    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.

  • @ahmed_aboelleef
    @ahmed_aboelleef18 күн бұрын

    thanks for sharing What is the editor theme name ?

  • @Webcrunch

    @Webcrunch

    18 күн бұрын

    Spacegray 80s I believe

  • @abdullahsaleh7196
    @abdullahsaleh719615 күн бұрын

    keep going 🫡