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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:48:23+00:00 2026-06-02T13:48:23+00:00

If I have a model like this: class Media attr_reader :title, :created_at end class

  • 0

If I have a model like this:

class Media
  attr_reader :title, :created_at
end

class Video < Media
end

class Picture < Media
  attr_reader :image_url
end

So, in the Picture class there is one attribute that should not exist in the superclass. What people usually do when you want to retrieve all the media? Would you want to throw exception on the method image_url? Or how would you re-model it?

  • 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-06-02T13:48:24+00:00Added an answer on June 2, 2026 at 1:48 pm

    If you’re interested in all media, then you shouldn’t care if it’s a video or a picture. In other words, a method that accepts a collection of Media should not call any methods from Media descendants. If you need all pictures, then work with Picture, don’t overgeneralize.

    If you still do need to process all media while telling the types apart, you can branch on a type.

    def process_media media
      media.each do |m|
        if m.is_a? Picture
          # work with picture
        elsif m.is_a? Video
          # work with video
        end
      end
    end
    

    An alternative is to use duck typing. That is, if it has a method image_url, then it must be some kind of a picture.

    def process_media media
      media.each do |m|
        if m.respond_to? :image_url
          # work with picture
        elsif m.respond_to? :bitrate
          # work with video
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple model like this one: class Artist(models.Model): surname = models.CharField(max_length=200) name
I have a view model like this: public class EditVM { public Media.Domain.Entities.Movie Movie
I have a model like this class Parent < ActiveRecord::Base :has_many :kids end class
I have a model like this: class Lesson include Mongoid::Document field :title, :type =>
I have a Model like this: class MyModel attr_accessor :size end Now i want
I have a model class like this: class Note(models.Model): author = models.ForeignKey(User, related_name='notes') content
I have a model.py like this: class EventTypeCategory(models.Model): name = models.CharField(max_length=50, verbose_name=Name) user =
I'm using MVC3 and I have a model like this: public class Foo {
I have a model that looks like this: class Invite(models.Model): user = models.ForeignKey(User) event
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A

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.