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

The Archive Base Latest Questions

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

I understand how STI works, in that I have say a Post model that

  • 0

I understand how STI works, in that I have say a Post model that
contains posts on a forum and several sub-posts like
‘ordinaryUserPost’ and ‘adminUserPost’ etc.

Now, I want to define the same method in each of the sub-posts, but
the method would do something different in each case, eg

class Post < ActiveRecord::Base
end

class AdminUserPost < Post
  def background_color
    'rockstar red'
  end
end

class OrdinaryUserPost < Post
  def background_color
    'pale blue'
  end
end

(yes its a silly example). Now in my thread controller I do Post.find
(:all) and it gives me a list of posts I need to render, but they are
‘Post’ objects, not AdminUserPost or OrdinaryUserPost – so I cannot
just get my background_color method! I would have to do a find on
each type of user post separately …

Is there anyway I can do:

Post.find(:all)

And in the resultant array get a list of AdminUserPost and
OrdinaryUserPost objects instead of Post objects?

Or is there a nice way of ‘casting’ my Post objects into AdminUserPost’s and OrdinaryUserPost’s as appropriate?

EDIT:

This works as expected – provided you have a column called ‘type’ in the Post class. If your column is called something else, such as ‘post_type’ then you need to add:

self.inheritance_column = 'post_type'

In ALL the child models (AdminUserPost and OrdinaryUserPost in this example) and in the parent (Post).

Thanks,

Stephen.

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

    Double check that the posts table has a column ‘type’ (string). If the AdminUserPosts and OrdinaryUserPosts are written to the table ‘posts’ and the type column is correct, you should get the subclass behavior you expect.

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

Sidebar

Related Questions

I understand that a class will have a VTABLE , if it contains at-least
I understand that if else staments should say stuff like: if dog equals one,
I understand that sessions in HTTP is stateless and hence we have methods like
I've hit something that I don't understand how to model with Rails associations and
I understand that it's possible (and I have done it) to return javascript and
I understand that some countries have laws regarding website accessibility. In general, what are
I understand that there are several ways to blend XNA and WPF within the
I understand that versioning in Mongoose is primarily intended to avoid issues with sub-document
I understand that the ST monad is something like a little brother of IO,
I understand that: '\n' // literally the backslash character followed by the character for

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.