# Coercion to child class? (between classes)

**URL:** <https://www.ruby-forum.com/t/coercion-to-child-class-between-classes/119231>\
**Category:** Rails\
**Created:** [November 5, 2007, 8:13am UTC](https://www.ruby-forum.com/t/coercion-to-child-class-between-classes/119231 "2007-11-05T08:13:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikhail\_Kornienko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@Mikhail\_Kornienko](https://www.ruby-forum.com/u/Mikhail_Kornienko)\
**Post date:** [November 5, 2007, 8:13am UTC](https://www.ruby-forum.com/t/coercion-to-child-class-between-classes/119231/1 "2007-11-05T08:13:30Z")

</div>

I guess this is a pretty basic thing, but I dug thru all the books I  
have and found nothing…

Basically, I have a CMS system which has Post class as a parent to  
Page, NewsPage and EventsPage classes.  
I need to read all the recently updated pages, news and events, and  
get an array of “posts” of correct class.

Here’s how it is implemented currently:

pages\_news\_events = Post.find(:all, :conditions =\> ‘post\_type in  
(“page”, “newspage”, “events\_page”)’, :select =\> “id,  
post\_type”).collect do |onepage|  
case onepage.post\_type  
when ‘page’  
Page.find\_by\_id(onepage.id)  
when ‘newspage’  
NewsPage.find\_by\_id(onepage.id)  
when ‘events\_page’  
EventsPage.find\_by\_id(onepage.id)  
end  
end

I basically just read the necessary minimum of “generic” Posts info  
from the database (id and type) and then “coerce” these records to  
full-fledged news/events/pages items depending on post\_type column.

What would be cool to have it the ability to get this arrays without  
additional find\_by\_ids so I won’t spawn lots of single-line queries.  
Coercion to child class or something. Unfortunately I was unable  
anything to achieve that.

Or may be there’s some Rails magic I can use in this case?

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kornienko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@Mikhail\_Kornienko](https://www.ruby-forum.com/u/Mikhail_Kornienko)\
**Post date:** [November 5, 2007, 11:14am UTC](https://www.ruby-forum.com/t/coercion-to-child-class-between-classes/119231/2 "2007-11-05T11:14:54Z")

</div>

On 5 Nov 2007, at 07:12, Mikhail Kornienko wrote:

> end  
> end
> 
> I basically just read the necessary minimum of “generic” Posts info  
> from the database (id and type) and then “coerce” these records to  
> full-fledged news/events/pages items depending on post\_type column.
> 
> What would be cool to have it the ability to get this arrays without  
> additional find\_by\_ids so I won’t spawn lots of single-line queries.  
> Coercion to child class or something. Unfortunately I was unable  
> anything to achieve that.

So do you have separate pages, events\_pages etc… tables ? If you  
could use single table inheritance then this would all go away.

You could also have a polymorphic association: you have a page\_type  
and page\_id column, say belongs\_to :page, :polymorphic =\> true.  
If you then do post.page then rails will essentially do the case  
statement you’ve got above. You can’t do an :include on a polymorphic  
belongs to though, although you can write some queries to the effect  
of 'get all page\_ids where page\_type = ‘news\_page’ and then load all  
NewsPages in one go with those ids.

Fred

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kornienko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@Mikhail\_Kornienko](https://www.ruby-forum.com/u/Mikhail_Kornienko)\
**Post date:** [November 5, 2007, 12:55pm UTC](https://www.ruby-forum.com/t/coercion-to-child-class-between-classes/119231/3 "2007-11-05T12:55:37Z")

</div>

I have all the pages stored in a single table, and distinct between  
them by “page\_type” column.  
But I wonder how do I do that table inheritance you are suggesting?

I currently have:  
class Post \< ActiveRecord::Base  
class Page \< Post  
class EventPage \< Post  
etc…

Guess I’m just slow at the end of the day, but I can’t come up with  
any code right now 😕 If you have a minute and it’s not too much of a  
hustle, could you show me a simple example of how you would do that?  
Would be much appreciated!

Thanks,  
Mike.

On Nov 5, 7:13 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kornienko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@Mikhail\_Kornienko](https://www.ruby-forum.com/u/Mikhail_Kornienko)\
**Post date:** [November 5, 2007, 1:14pm UTC](https://www.ruby-forum.com/t/coercion-to-child-class-between-classes/119231/4 "2007-11-05T13:14:54Z")

</div>

Ah indeed! Thanks! 🙂

Mike.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kornienko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@Mikhail\_Kornienko](https://www.ruby-forum.com/u/Mikhail_Kornienko)\
**Post date:** [November 5, 2007, 1:08pm UTC](https://www.ruby-forum.com/t/coercion-to-child-class-between-classes/119231/5 "2007-11-05T13:08:29Z")

</div>

On 5 Nov 2007, at 11:54, Mikhail Kornienko wrote:

> Guess I’m just slow at the end of the day, but I can’t come up with  
> any code right now 😕 If you have a minute and it’s not too much of a  
> hustle, could you show me a simple example of how you would do that?  
> Would be much appreciated!

if you have a type column, then rails will use that to instantiate the  
correct subclass when the row is loaded.

Gred
