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 6234517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:30:01+00:00 2026-05-24T10:30:01+00:00

I’m factoring out the meat of an ActiveRecord class A into a module M

  • 0

I’m factoring out the meat of an ActiveRecord class A into a module M, so that I’m able to duplicate certain data pipelines. The original A had AR macro methods such as belongs_to :X. Although I’m able to separate out non-AR things fine into the module and mix it back into A, the module does not know anything about belongs_to or B out of the box. How do I make those available to the module, and then mix it back into the new shallow A, which only includes M now, and B, which is a clone of A with its own underlying AR table? Or should I write something like an acts_as plugin (right?) instead of M? Retaining belongs_to in A and duplicating it in B works, but defeats the DRY…

  • 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-24T10:30:02+00:00Added an answer on May 24, 2026 at 10:30 am

    When to create a module and when to use inheritance?
    A question came up today that got me thinking about how much Ruby on Rails developers really understand about the tools they use.

    The question related to refactoring two models that shared functionality. A common enough requirement and a very sensible thing to want to do but the comments and solutions raised my eyebrows some what.
    This was the question, somewhat edited and reformatted to make it clearer

    I’m factoring out the meat of an ActiveRecord class A into a module M,
    so that I’m able to duplicate certain data pipelines.

    The original A model had ActiveRecord macro methods such as belongs_to
    :X.

    Although I’m able to separate out non-AR things fine into the module
    and mix it back into A, the module does not know anything about
    belongs_to or anything about AR model B out of the box.

    How do I make those available to the module, and then mix it back into
    the new shallow A, which only includes M now, and B, which is a clone
    of A with its own underlying AR table? Or should I write something
    like an acts_as plugin (right?) instead of M? Retaining belongs_to in
    A and duplicating it in B works, but defeats the DRY principle

    What I didn’t understand was why the person asking the question was putting this code into a module instead of into a class that the models could descend from.

    In Rails (almost) every class descends from another class right?
    You see code like

    class MyModel < ActiveRecord::Base
    

    all over the place. Fine that ::Base might seem a little mysterious and I can see how that kind of hides what is going on here so lets look at the controller example

    All controllers descend from ApplicationController when first generated right?
    So you get

    class MyController < ApplicationController
    

    How many of you put code into the application controller like before filters and current)user methods and end up using that code in your controllers and views?
    Once you take time to think about it a bit then you can see that if you put code in ApplicationController that is public or protected then all controllers that descend from ApplicationController get that functionality right?

    ApplicationController is just a class that descends from ActionController::Base the definition looks like this

    class ApplicationController < ActionController::Base
    

    Now that looks so familiar the above usage is so common that I start to think that a lot of Rails developers struggle to see the wood for the trees.

    This is all about inheritance.

    Rails puts a bunch of methods into the ActionController::Base and ActiveRecord::Base classes (That’s all they are, classes inside a module) so you can descend your own classes from these classes thereby inheriting the methods and functionality provided by these base classes.

    So why not create an abstract ActiveRecord::Base class to solve the problem. This seemed to me the most totally obvious and natural approach to take.

    I came up with this solution

    In a_base_class.rb
    
    class ABaseClass < ActiveRecord::Base
      abstract true
      has_many  :whatevers
      belongs_to :whatevers
    
      def common_methods
        #some code here
      end
    
    end
    
    Then class a
    
    class A < ABaseClass
      # Whatever
    end
    

    This could be placed inside a module for namespacing purposes

    If you want to put that in a module then descend from WhateverModule::ABaseClass then that’s a cool way of name spacing the new base class so that class names don’t conflict and that is one of the main purposes of using a module. To name space classes.

    Obviously use whatever real class names that make sense to you.

    @Rishav Rastogi provided a great answer for using modules and this is what got mne really wondering why thwe solution was not so clear to others and why the question had even been asked in the first place and I started to get the impression that people don’t really know what code like this really does

    class SomeController < ApplicationController
    

    and

    class MyModel < ActiveRecord::Base
    

    When this is stuff that Rails developers use every day?

    It’s all about inheritance.

    Abstract and non abstract classes all inherit from a single class right? The class being inherited from may well inherit from a number of other classes forming a single inheritance chain but it’s still single inheritance. each class only descends from one other class.

    So what can modules do to help?
    Modules are somewhat confusingly used for 2 purposes.
    1) To name space things as already mentioned
    2) To provide a multiple inheritance scenario. Multiple inheritance is a dirty word in the development world. Things can end up in a right mess but modules provide quite a neat solution.

    An example of why you would want multiple inheritance

    ActiveRecord::Base provides methods like find_by_something and find.all that return an array of ActiveRecord::Base objects (classes are the code objects are actual things)

    Knowing this it would make sense to have the Base class inherit from the array class, but if it did that then it wouldn’t be able to inherit from any other more appropriate class. The solution is to mix in a module. If the module contains the array class you get all the juice of array functionality such as .each and .empty? plus all the juice of the other classes that ActiveRecord::Base uses.

    So when to use a module and when to inheritance?

    Use a module for name spacing (classes can live inside a module)

    Use a class for inheritance

    So use them both together at the same time unless you want multiple inheritance in which case just use modules

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words

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.