# Single table inheritance and find(:all) in parent

**URL:** <https://www.ruby-forum.com/t/single-table-inheritance-and-find-all-in-parent/165076>\
**Category:** Rails\
**Created:** [April 8, 2009, 9:38pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-and-find-all-in-parent/165076 "2009-04-08T21:38:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [April 8, 2009, 9:38pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-and-find-all-in-parent/165076/1 "2009-04-08T21:38:34Z")

</div>

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?

Thanks,

Stephen.

---

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [April 9, 2009, 12:42am UTC](https://www.ruby-forum.com/t/single-table-inheritance-and-find-all-in-parent/165076/2 "2009-04-09T00:42:36Z")

</div>

On Apr 8, 8:37 pm, “stephen O’D” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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?

That’s the way it should work out of the box. Would be interesting to  
see the data / code that results in this not happening.

Fred

---

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [April 10, 2009, 6:35pm UTC](https://www.ruby-forum.com/t/single-table-inheritance-and-find-all-in-parent/165076/3 "2009-04-10T18:35:40Z")

</div>

> That’s the way it should work out of the box. Would be interesting to  
> see the data / code that results in this not happening.

I was being stupid - put my type column was actually called post\_type,  
and I added self.inheritance\_column = '‘post\_type’ in all the child  
classes, but forgot the parent!
