Some kind of polymorphism.

Hello out there,

let's assume, I've got an article model. An article always has an
author and refers to it. But I've got several types of authors which
all have theier own models. So a simple foreign key rather belongs_to
will not help me. So, I could save the author_id and a kind of
authot_type to manually create the association. Is there a more
comfortable way in rails?

Thank you very much! :slight_smile:
ms

Read this: http://wiki.rubyonrails.org/rails/pages/polymorphicassociations

Thank you for your answer. I read this article before, but I can't map
this technique to my problem. Maybe you can help me with this?

Thank you!

class Article < ActiveRecord::Base
  belongs_to :author, :polymorphic => true
end

And then to assign an author:

article = Article.new
article.author = some_author
article.save

BTW, your table should have an "author_id" column (usually an integer)
and an "author_type" column (usually a varchar).

> class Article < ActiveRecord::Base
> belongs_to :author, :polymorphic => true
> end

> And then to assign an author:

> article = Article.new
> article.author = some_author
> article.save

But I've got no no author model, since author is nothing concrete in
my data model. Doesn't this matter?

No, the "author" can be any of you models that descend from ActiveRecord::Base.

Hey, fantastic thing and so easy, thank you!

Polymorphic associations

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

Why not?

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

Um dude you're talking single table inheritance not polymorphic
associations which is what he's after

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/