Coercion to child class? (between classes)

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?

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

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 :confused: 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.

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 :confused: 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

Ah indeed! Thanks! :slight_smile:

Mike.