Rails Concerns vs. Modules and when to use each

Описание к видео 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: https://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 (https://webcrunch.com), where I publish tutorials, articles, and the occasional vlog about design and development.

📰 Never miss an update! Click here to subscribe: https://www.youtube.com/user/webcrunc...

💻 Read the written version:
https://webcrunch.com/posts/

💎 New to Ruby on Rails? Enroll in my course HELLO RAILS:
https://hellorails.io

💻 The Blog (my source of truth): https://webcrunch.com

Additional Links:
🎨 Bring life to Rails projects with Rails UI: https://railsui.com.
👨‍💻 Hire me: Visit: https://railsui.com/custom
🐤   / webcrunchblog  .
⚙️ https://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!

✨ https://github.com/sponsors/justalever
☕️ https://www.buymeacoffee.com/webcrunch

Комментарии

Информация по комментариям в разработке