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

  • Home
  • SEARCH
  • 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 6104967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:54:11+00:00 2026-05-23T13:54:11+00:00

I have an ActiveRecord model Media which is supposed to be able to store

  • 0

I have an ActiveRecord model Media which is supposed to be able to store similarly structured information about different types of media (Media::Book, Media::Movie, Media::Music). However each of these subclasses has unique methods.

# TABLE medias
# string :title
# string :description
# integer :media_type

class Media < ActiveRecord::Base
end

class Media
  class Book < Media
    def reviews
      GoogleBooks.search(name).get_reviews
    end
  end
  class Movie < Media
    def reviews
      IMDB.search_movies(name).reviews
    end
  end
  class Music < Media
    def reviews
      Lastfm.search(name).comments
    end

    def music_video
      Youtube.search(name).first.embed_html
    end
  end
end

This would work if I used Media::Book.new("Harry Potter").reviews, but I want to be able to use

Media.find("Harry Potter")
=> Media::Book

and

Media.find("Harry Potter").reviews

I know I should use media_type to achieve this, but I’m not sure if theres a better way to do it than overriding every single ActiveRecord database interface method (User.medias, find, find_by_etc, where, order, limit, all) and replacing each of their return values.

  • 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-23T13:54:12+00:00Added an answer on May 23, 2026 at 1:54 pm

    you can use ActiveRecords Single Table Inheritance feature for that.
    It uses another column on your models table (defaults to a column named “type”) to determine the type of the model for every record.

    Just add a string type column to your medias table and Rails will do the magic for you when it finds that column in your databases schema.
    If you are fine with AR adding the type to your media_type column, you can also change the column used for Single Table Inheritance using the set_inheritance_column class method.

    class Media < ActiveRecord::Base
      set_inheritance_column :media_type
    end
    

    Then you’ll find the class name of the corresponding object with its full namespaced classname in this column. (eg.: “Media::Book”) That said, you don’t wanna change the content of the type column (or whatever column you use) manually. ActiveRecord will take care of it by itself.

    Have a look at http://api.rubyonrails.org/classes/ActiveRecord/Base.html and search for “Single table inheritance” within that page for further information

    Edit:
    just realized your media_type column is an integer. So using it for STI won’t work. Just stick to a string type column and let AR do the rest for you.

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

Sidebar

Related Questions

I have an ActiveRecord model, Foo , which has a name field. I'd like
I have a basic ActiveRecord model in which i have two fields that i
I have an ActiveRecord model that I would like to convert to xml, but
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
I have an ActiveRecord model Language , with columns id and short_code (there are
I have an ActiveRecord model whose fields mostly come from the database. There are
I have an ActiveRecord model with a field called name (in the database) and
I have an ActiveRecord model class Foo that has_many Bar . I want to
I have an ActiveRecord model with several virtual attribute setters. I want to build

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.