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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:25:49+00:00 2026-05-19T16:25:49+00:00

Preface: This is in the context of a Rails application. The question, however, is

  • 0

Preface: This is in the context of a Rails application. The question, however, is specific to Ruby.

Let’s say I have a Media object.

class Media < ActiveRecord::Base
end

I’ve extended it in a few subclasses:

class Image < Media
  def show
    # logic 
  end
end

class Video < Media
  def show
    # logic 
  end  
end

From within the Media class, I want to call the implementation of show from the proper subclass. So, from Media, if self is a Video, then it would call Video’s show method. If self is instead an Image, it would call Image’s show method.

Coming from a Java background, the first thing that popped into my head was ‘create an abstract method in the superclass’. However, I’ve read in several places (including Stack Overflow) that abstract methods aren’t the best way to deal with this in Ruby.

With that in mind, I started researching typecasting and discovered that this is also a relic of Java thinking that I need to banish from my mind when dealing with Ruby.

Defeated, I started coding something that looked like this:

def superclass_method
  # logic
  this_media = self.type.constantize.find(self.id)
  this_media.show      
end

I’ve been coding in Ruby/Rails for a while now, but since this was my first time trying out this behavior and existing resources didn’t answer my question directly, I wanted to get feedback from more-seasoned developers on how to accomplish my task.

So, how can I call a subclass’s implementation of a method from the superclass in Rails? Is there a better way than what I ended up (almost) implementing?

  • 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-19T16:25:50+00:00Added an answer on May 19, 2026 at 4:25 pm

    Good question, but you are making it too complicated. Keep in mind a few principles and it should all be clear…

    • The types will be resolved dynamically, so if a show exists anywhere in the object’s class hierarchy at the moment it is actually called then Ruby will find it and call it. You are welcome to type in method calls to anything that may or may not exist in the future and it’s legal ruby syntax and it will parse. You can type in an expression that includes a reference to this_will_never_be_implemented and no one will care unless it actually gets called.

    • Even in Java, there is only one actual object. Yes, you may have a method in the superclass that’s calling a method, but it is an instance of the derived class (as well as an instance of the base class) and so you can count on the new show being called.

    • In a sense, every Ruby class is an abstract class containing stubs for every possible method that might be defined in the future. You can call anything without access qualifiers in the base class or derived class.

    If you want a null superclass implementation, you may want to define one that does nothing or raises an exception.

    Update: Possibly, I should have just said “call show like any other method” and left it at that, but having come this far I want to add: You can also implement show with Ruby’s version of multiple inheritance: include SomeModule. Since you are obviously interested in Ruby’s object model, you might implement your attribute with a mixin just for fun.

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

Sidebar

Related Questions

Let me preface this question. I have just started using jquery, so please be
Let me preface this question by saying I've exhausted Google, or at least what
I'll preface this question by stating I have historically used tables for my HTML
Please let me preface this question with an apology. I am very new to
Let me preface this question by stating that i don't usually condone the practice
Let me preface this with.. I have extremely limited experience with ASM, and even
First let me preface this question by saying that I'm fairly new to Javascript.
Let me preface this question by saying I use TextMate on Mac OSX for
Let me preface this by saying I'm a total rails noob. I've just written
To preface this question, this is my first app and I have a very

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.