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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:16:14+00:00 2026-05-13T16:16:14+00:00

I’d like to be able to describe different types of a model using RoR

  • 0

I’d like to be able to describe different types of a model using RoR associations. An example:

Models:
Post

ImagePost
post_id:integer
url:string

MessagePost
post_id:integer
message:string

ImagePost and MessagePost are a type of Post. I’d like @posts = Post.all to retrieve both types of post and allow me access to their attributes via @posts.url or @posts.message.

I’m sure I’m missing something simple, please enlighten me!

Cheers,

Ben.

  • 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-13T16:16:14+00:00Added an answer on May 13, 2026 at 4:16 pm

    You could try the mixed model approach, but it’s a fair amount of work to set up. It’s bit of of a kludge that trades performance for more efficient database storage.

    The idea is that you use STI to handle all the common Post fields, and delegate the unique fields for each subclass to an another table and eager load that association.

    The base Post class could look like this. Note the class_eval could be abstracted into a module that gets included and exended into a subclass.

    #columns: id:integer, timestamps, user_id:integer, 
    #  topic_id:integer, type:string
    class Post < ActiveRecord::Base
      # common methods/validations/associations
      belongs_to :user
      belongs_to :topic 
    
      def self.relate_to_detail
        class_eval <<-"EOF"
          has_one :detail, :class_name => "#{self.name}Detail"
          accepts_nested_attributes_for :detail
    
          default_scope :include => :detail
    
          def method_missing(method, *args)
            build_detail if detail.nil?
            if detail && detail.respond_to?(method, true)
              detail.send(method, *args)
            else 
              super(method, *args)
            end
          end
    
          def respond_to?( method, include_private = false)
            build_detail if detail.nil?
            super(method, include_private) || 
              detail.respond_to?(method, include_private)
          end
        EOF
      end
    end
    

    Then you’ll need to define the sub and detail class for each type.

    #uses posts table
    class ImagePost < Post
      relate_to_detail
    end
    
    #columns: id, image_post_id, url:string, height:integer, :width:integer
    class ImagePostDetail < ActiveRecord::Base
      belongs_to :image_post
    end
    
    #uses posts table
    class MessagePost < Post
      relate_to_detail
    end
    
    #columns: id, message_post_id, message:string
    class MessagePostDetail < ActiveRecord::Base
      belongs_to :image_post
    end
    

    Now we can do things like this:

    @user.message_posts.create(:message => "This is a message")
    @image_post.url
    

    Which will create a new MessagePost, where the user_id, timestamps, post_id, post_type are all stored in the posts table, while the message is stored in the MessagePostDetails table and return the url of an ImagePost respectively.

    The new method_missing and respond_to? definitions work the magic to hide the division.

    @Post.all will now list Posts of all types. The only downside is that the detail fields will not be displayed when you fetch a post.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have some data like this: 1 2 3 4 5 9 2 6
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I have text I am displaying in SIlverlight that is coming from a CMS
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small

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.