Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 559803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:16:35+00:00 2026-05-13T12:16:35+00:00

What are the advantages and disadvantages of creating a module like: module Section def

  • 0

What are the advantages and disadvantages of creating a module like:

module Section
  def self.included(base)
    base.class_eval do
      has_many :books
    end
  end

  def ensure_books
    return false if books <= 0
  end
end

…where ActiveRecord methods are used in the module instead of directly on the class(es) they belong to?

Should modules be used for methods like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:16:35+00:00Added an answer on May 13, 2026 at 12:16 pm

    The most obvious advantage is that you can take functionality that is shared and put it into a single place. This is just a general advantage of keeping your code organized and modularized (no pun intended) – and you should, of course, do that

    Using Active Record methods does not make these Modules special in any way.

    The most obvious disadvantage is that your code, as written, is a little more complex. You can’t use validates_presence_of in a module directly because it does not inherit from ActiveRecord::Base. (Rails 3 is supposed to make it easier to selectively extend your own classes/modules with bits of ActiveRecord-Functionality

    Instead, you need to call the Active-Record-Methods on your model class when your model is included:

    module FooHelper
      def self.included(other)
        other.send(:has_many, :foos)
      end
    end
    

    So the prime disadvantage is that your code gets a little harder to read.

    If you are just breaking up a single class into separate parts and don’t need to reuse the code somewhere else, you could use the concerned_with-pattern which works by reopening classes.

    On the other hand, If you need more functionality, like configuration parameters for your extension, consider writing a plugin

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the actual difference, advantages and disadvantages, of creating a new event handler,
What would be the advantages and disadvantages of creating external silverlight modules for dynamic
I'd like to know more about the advantages and disadvantages of forking a github
What are the advantages/disadvantages of int and Int32 ? And Why should we use
What is the difference (advantages / disadvantages of both of them) between OracleCachedRowSet and
What are the advantages/disadvantages in placing a lengthy network access code in a thread
What are the advantages/disadvantages of Option 2 over 1 in this example? Option 1
Can somebody explain the main differences between (advantages / disadvantages) the two implementations? For
How can I use std::shared_ptr for array of double? Additionally what are advantages/disadvantages of
What advantages and disadvantages using nginx+Apache+mod_wsgi vs nginx+uWSGI(vurtualenv) in production Advantages of first variant

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.