Forum: Ruby on Rails Polymorphic associations. Agile rails book confusion.

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.
7b9848f7452ca6a1f681e68370577b1c?d=identicon&s=25 Michael Chow (nanostuff)
on 2007-06-02 06:51
On page 350 in the PDF it mentions that having multiple tables have a
has_one relationship to the same table cannot work, that is,

class Article
has_one :catalog_entry
end

class Sound
has_one :catalog_entry
end

class Image
has_one :catalog_entry
end

I quote, "We can't possibly arrange to have the foreign key in the
catalog entry point back to all three tables"

If I understand correctly, it says catalog_entry cannot contain a
reference to all three tables. My question is, why not?

If catalog_entry has two foreign keys, image_id and sound_id, wouldn't
all the following calls work?

Image.catalog_entry
Sound.catalog_entry
CatalogEntry.image
CatalogEntry.sound

I cannot see the problem the book suggests is there, what am I missing?
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2007-06-02 10:47
Michael Chow wrote:
> On page 350 in the PDF it mentions that having multiple tables have a
> has_one relationship to the same table cannot work, that is,
>
> class Article
> has_one :catalog_entry

>
> Image.catalog_entry
> Sound.catalog_entry
> CatalogEntry.image
> CatalogEntry.sound
>
> I cannot see the problem the book suggests is there, what am I missing?

You have to use blongs_to when you use has_one, that's why it's not
working :)
7b9848f7452ca6a1f681e68370577b1c?d=identicon&s=25 Michael Chow (nanostuff)
on 2007-06-02 11:53
Jamal Soueidan wrote:
> You have to use blongs_to when you use has_one, that's why it's not
> working :)
Oh sure, I know, but...

class CatalogEntry
belongs_to :image
belongs_to :sound
belongs_to :article
end

I've not used multiple belongs_to associations in a single class yet,
but I can't see why this wouldn't work as long as there are foreign keys
for each one.

In this case the catalog entry would be pointing to all three tables,
would it not?
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2007-06-02 12:35
Michael Chow wrote:
> Jamal Soueidan wrote:
>> You have to use blongs_to when you use has_one, that's why it's not
>> working :)
>
> I've not used multiple belongs_to associations in a single class yet,
> but I can't see why this wouldn't work as long as there are foreign keys
> for each one.
>
> In this case the catalog entry would be pointing to all three tables,
> would it not?

Can I see your tables :)

class Blog
   has_many :comments
end

class Comments
   belongs_to :blog
end

The comments table MUST have a columns named "blog_id", or else the
association would not work.
6ebedec9056fcd5ecff378068cf03f83?d=identicon&s=25 Jimtron (Guest)
on 2007-06-02 18:02
(Received via mailing list)
I think the goal is to get something like CatalogEntry.resource to
accept or return a number of different objects. If polymorphic
associations are being used than we could do something like this:

CatalogEntry.resource = Image.new

or

CatalogEntry.resource = Sound.new

or

CatalogEntry.resource = Video.new

Normal associations won't allow that.

Jim



On Jun 1, 10:51 pm, Michael Chow <rails-mailing-l...@andreas-s.net>
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2007-06-02 18:04
Understanding Polymorphic Associations
http://wiki.rubyonrails.org/rails/pages/Understand...

You get some examples how it works :)
7b9848f7452ca6a1f681e68370577b1c?d=identicon&s=25 Michael Chow (nanostuff)
on 2007-06-02 22:56
Its really the "We cant possibly arrange to have the foreign key in the
catalog entry point back to all three tables" part that confused me. As
far as I can see, you can point back to all three, you just have to add
more foreign keys... But I think I'm starting to see the point, the idea
is to point back to either of the three without using more foreign keys.
That is, so you can access the resource without necessarily knowing what
the resource is.

If I'm making sense of this correctly, it would make better sense to use
regular associations if the catalog entry is associated with all three
classes, but use polymorphic if it can be either of the three. If I have
this all backwards, let me know, otherwise thanks :)
Ff168162d53e22788d576582b3527e97?d=identicon&s=25 Bill Kocik (Guest)
on 2007-06-03 00:16
(Received via mailing list)
On 6/2/07, Michael Chow <rails-mailing-list@andreas-s.net> wrote:

> If I'm making sense of this correctly, it would make better sense to use
> regular associations if the catalog entry is associated with all three
> classes, but use polymorphic if it can be either of the three. If I have
> this all backwards, let me know, otherwise thanks :)


Nope - looks like you've got it. To clear up your initial confusion,
when they say "can't arrange to have the foreign key point back to all
three tables", think of it as "can't arrange to have *one single*
foreign key point back to all three tables". Of course you can do it
with three foreign keys, but as you now understand, that isn't the
point of polymorphic associations.

--
Bill Kocik

http://bkocik.net
This topic is locked and can not be replied to.