Forum: Ruby on Rails Some kind of polymorphism.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6f3f4fcb842db4b755cd8adc8bca67c6?d=identicon&s=25 ms (Guest)
on 2009-02-08 19:56
(Received via mailing list)
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! :)
ms
A91bd6cef23eb3516245a092e196c4da?d=identicon&s=25 Maurício Linhares (mauricio)
on 2009-02-08 20:02
(Received via mailing list)
6f3f4fcb842db4b755cd8adc8bca67c6?d=identicon&s=25 ms (Guest)
on 2009-02-08 20:07
(Received via mailing list)
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!
A91bd6cef23eb3516245a092e196c4da?d=identicon&s=25 Maurício Linhares (mauricio)
on 2009-02-08 20:09
(Received via mailing list)
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

-
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)
A91bd6cef23eb3516245a092e196c4da?d=identicon&s=25 Maurício Linhares (mauricio)
on 2009-02-08 20:12
(Received via mailing list)
BTW, your table should have an "author_id" column (usually an integer)
and an "author_type" column (usually a varchar).

-
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)



On Sun, Feb 8, 2009 at 4:09 PM, Maurício Linhares
6f3f4fcb842db4b755cd8adc8bca67c6?d=identicon&s=25 ms (Guest)
on 2009-02-08 20:19
(Received via mailing list)
> > 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?
A91bd6cef23eb3516245a092e196c4da?d=identicon&s=25 Maurício Linhares (mauricio)
on 2009-02-08 20:20
(Received via mailing list)
No, the "author" can be any of you models that descend from
ActiveRecord::Base.

-
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)
6f3f4fcb842db4b755cd8adc8bca67c6?d=identicon&s=25 ms (Guest)
on 2009-02-08 20:22
(Received via mailing list)
Hey, fantastic thing and so easy, thank you!

On 8 Feb., 20:19, Maurício Linhares <mauricio.linha...@gmail.com>
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-09 05:28
(Received via mailing list)
Polymorphic associations

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-09 05:30
(Received via mailing list)
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-09 05:32
(Received via mailing list)
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/

On 09/02/2009, at 6:19 AM, Maur¨ªcio Linhares
<mauricio.linhares@gmail.co
This topic is locked and can not be replied to.