Polymorphic associations. Agile rails book confusion

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?

Michael C. 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 :slight_smile:

Jamal S. wrote:

You have to use blongs_to when you use has_one, that’s why it’s not
working :slight_smile:
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?

Michael C. wrote:

Jamal S. wrote:

You have to use blongs_to when you use has_one, that’s why it’s not
working :slight_smile:

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 :slight_smile:

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.

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 C. [email protected]

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 :slight_smile:

Understanding Polymorphic Associations
http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations

You get some examples how it works :slight_smile:

On 6/2/07, Michael C. [email protected] 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 :slight_smile:

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 K.

http://bkocik.net