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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:16:09+00:00 2026-05-12T23:16:09+00:00

Class A and B are identical: class A < ActiveRecord::Base def foo puts foo

  • 0

Class A and B are identical:

class A < ActiveRecord::Base
 def foo
  puts "foo"
 end
end

class B < ActiveRecord::Base
 def foo
  puts "foo"
 end
end

What’s the difference between refactoring like this with a base class:

class Base < ActiveRecord::Base
 def foo
  puts "foo"
 end
end

class A < Base
end

class B < Base
end

versus like this using a base module:

module Base
 def foo
  puts "foo"
 end
end

class A < ActiveRecord::Base
 include Base
end

class B < ActiveRecord::Base
 include Base
end

Is one way preferable over another?

  • 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-12T23:16:09+00:00Added an answer on May 12, 2026 at 11:16 pm

    There’s a fundamental difference between those two methods that all the other answers are missing, and that’s rails’ implementation of STIs (Single Table Inheritance):

    http://api.rubyonrails.org/classes/ActiveRecord/Base.html (Find the “Single Table Inheritance” section)

    Basically, if you refactor your Base class like this:

    class Base < ActiveRecord::Base
      def foo
        puts "foo"
      end
    end
    
    class A < Base
    end
    
    class B < Base
    end
    

    Then, you are supposed to have a database table called “bases”, with a column called “type”, which should have a value of “A” or “B”. The columns on this table will be the same across all your models, and if you have a column that belongs to only one of the models, your “bases” table will be denormalized.

    Whereas, if you refactor your Base class like this:

    Module Base
      def foo
      puts "foo"
     end
    end
    
    class A < ActiveRecord::Base
     include Base
    end
    
    class B < ActiveRecord::Base
     include Base
    end
    

    Then there will be no table “bases”. Instead, there will be a table “as” and a table “bs”. If they have the same attributes, the columns will have to be duplicated across both tables, but if there are differences, they won’t be denomarlized.

    So, if one is preferable over the other, yes, but that’s specific to your application. As a rule of thumb, if they have the exact same properties or a big overlap, use STI (1st example), else, use Modules (2nd example).

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

Sidebar

Ask A Question

Stats

  • Questions 231k
  • Answers 231k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For NodeList collections you can use .item(): var allElements =… May 13, 2026 at 2:08 am
  • Editorial Team
    Editorial Team added an answer Without knowing all the details of the protocol, I would… May 13, 2026 at 2:08 am
  • Editorial Team
    Editorial Team added an answer Yes, Joomla would be fine for a newspaper site. You… May 13, 2026 at 2:08 am

Related Questions

I have a class which is basically a copy of another class. public class
This may be a little subjective, but I have often found that it can
I'm having issues with a very strange error in some code I wrote. The
I spend much of my time programming in R or MATLAB. These languages are
NHibernatians! I have a table [dbo].[Wibble] and another table [dbo].[WibbleExtended]. [Wibble] is the main

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.