Topic: Polymorphic association to STI model: find(:all, :include) fails

Can someone help me figure out why this won' work? please?

class Tip < Topic

class Article < Topic

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

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

  def list
     @comments = Comment.find(:all, :include => 'topic')
                                        #or :topic or 'topics' or :topics all fail
  end

# YIELDS THE FOLLOWING ERROR:
# ActiveRecord:ConfigurationError: Association named 'topic' was not found;

Re: Polymorphic association to STI model: find(:all, :include) fails

Try:

@comments = Comment.find(:all, :include => 'commentables')

Railscasts - Free Ruby on Rails Screencasts

Re: Polymorphic association to STI model: find(:all, :include) fails

That yields a:

ActiveRecord::EagerLoadPolymorphicError...

WTH is that???

Re: Polymorphic association to STI model: find(:all, :include) fails

It doesn't look like Rails supports including polymorphic associations in queries. This is probably because the join is dependent upon the data that is inside the row, so a separate join would have to be made for each row - a little beyond what a single SQL query can do.

Railscasts - Free Ruby on Rails Screencasts

Re: Polymorphic association to STI model: find(:all, :include) fails

Ok thanks a lot.

Just for future reference (I've been struggling with this problem for about 6 hours now)
where is a good place to look to get a better understanding of the types of methods you can do with polymorphic associations?  Should I just go directly to the code?  Because most of the rest of the information out there isn't terribly relevant most of the time.

Re: Polymorphic association to STI model: find(:all, :include) fails

The Rails Wiki has quite a bit on the subject, but the information is very scattered. The 2nd edition of AWDwR goes into the subject too, but I don't know anywhere that specifies what exactly can't be done with a polymorphic association which is possible with a normal one.

Railscasts - Free Ruby on Rails Screencasts

Re: Polymorphic association to STI model: find(:all, :include) fails

Yeah, I've read through that too.  I guess I just have to keep in mind that it's still a work in progress and not make too many assumptions that what I'm doing is possible with rails.  It's the second time I've run into this problem (with STI and polymorphic relations).  Maybe I should give it up smile

Re: Polymorphic association to STI model: find(:all, :include) fails

Yeah, they definitely aren't perfect, but I don't know of a better alternative. If you do give up on them, what would you do instead?

Railscasts - Free Ruby on Rails Screencasts

Re: Polymorphic association to STI model: find(:all, :include) fails

Oh know i mean give up on trying to do polymorphism with STI without using find_by_sql