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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:45:27+00:00 2026-05-30T18:45:27+00:00

Suppose I’m creating a blog with specific types of posts. I have a model

  • 0

Suppose I’m creating a blog with specific types of posts.

I have a model called an Item. An Item has a name (string) and a link (string) to a source or something. An Item has_and_belongs_to_many :tags (tags are just strings stored in their own database).

I want to extend Items into two different models: Quote and Link.

A Quote is an Item with some content (text) attached to it.

A Link is pretty much the same as an Item.

Both Texts and Links need to have names and links from the Item class, but a Quote has an extra field (the content:text). I’m only giving Link a subclass because it’s not supposed to be thought of as a superclass of Quote.

I want to be able to display Links and Quotes on their own pages, and render them differently. I also want to be able to list all Items on the front page, and render them differently (Quotes need to show their content).

Can anyone help with this?

  • 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-30T18:45:29+00:00Added an answer on May 30, 2026 at 6:45 pm

    This is a pretty simple case of ActiveRecord inheritance, and STI (Single Table Inheritance) should suite your needs just fine.

    All you really need to do is make sure that your items table has all the necessary fields to cover each of its descendants’ attributes, plus a type (string) field, which is used internally by ActiveRecord to keep track of the class of each record.

    In this case, it looks like you need to add a content (text) field and the type (string) field to the items table in the database.

    Once you have the right fields, go ahead and subclass Item:

    class Quote < Item
      ...
    end
    

    etc, etc.

    I recommend you use a single ItemsController for both types and keep as much functionality as possible in the parent model, as this generally keeps things cleaner and simpler.

    I would add show views for each of the subclasses in their own view folder, app/views/links for example, so that you can display them differently on their own pages.

    I would also add a app/views/links/_link partial for each of the types, which Rails will intelligently render when you call render @item on the items#index page, for example.

    # items/index.html.haml
    - @items.each do |item|
      = render item
    

    The above will render app/views/links/_link.html.haml for Link records and app/views/quotes/_quote.html.haml for Quote records.

    This should make it pretty easy to achieve the functionality you describe.

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

Sidebar

Related Questions

Suppose I have the following table: PERSON : ID INT NAME STRING LASTNAME STRING
Suppose we have the Lyric model: class Lyric < ActiveRecord::Base belongs_to :song end and
Suppose I have a innerclass extends asynctask called A and the outerclass extends Activity
Suppose I have a struct called Node as follows: struct foo { foo *next;
Suppose I have a superclass Item and a subclass MovingItem. If I create an
Suppose I have a FruitDetector class which takes in a string and returns the
Suppose that I have an n -sided loaded die, where each side k has
Suppose I have a model: class SomeModel(models.Model): id = models.AutoField(primary_key=True) a = models.CharField(max_length=10) b
suppose I have {data: {243232: {id: testid,name: test } }} so how to get
Suppose I have a string which contains an HTML file. How do I get

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.