Topic: Switching table to STI breaks polymorphic magic...

I tried switching a table to a STI table by adding 'type' and changing the name of the table/class...  Then adding the inherited class, now all of a sudden the polymorphic relationship fails to work.  Here's the relevant code:

class Topic < ActiveRecord::Base
  has_many  :comments, :as => :commentables

class Tip < Topic
...

class Comment < ActiveRecord::Base
  belongs_to  :commentables, :polymorphic => true

...

class TopicsController < ApplicationController
  def show
    if (params[:id])
      @topic = Topic.find(params[:id])
    else
      @topic = Topic.random
    end 
   
    @comments = Topic.find(@topic.id).comments
  end

...

Now, when I try to access @comments it's always empty.
I've tried every combination of   has_many  :comments, :as => :commentables
in Topic & Tip to no avail.  As well as moving the "show" code into

class TipsController < TopicsController


Any ideas?

-xdotcommer

Re: Switching table to STI breaks polymorphic magic...

Ok, it seems to work ok now... The commentables_type wasn't updating the new value for some reason.

Re: Switching table to STI breaks polymorphic magic...

Arg I still have problems with this.  It only works when the "commentable_type" is "topic" the parent class... is it possible to have it work with the subclass.

That is, "commentable_type" = "Tip"

Now I can do Tip.find(topic_id).comments

It's not working that way as it stands.

But, Topic.find(topic_id).comments works ok

Re: Switching table to STI breaks polymorphic magic...

If anyone else runs into an issue with this, it turns out
you must use the base model, not the inherited one.

Here are the details I was able to find:

http://dev.rubyonrails.org/attachment/t … ation.diff