# Polymorphic associations. Agile rails book confusion

**URL:** <https://www.ruby-forum.com/t/polymorphic-associations-agile-rails-book-confusion/105758>\
**Category:** Rails\
**Created:** [June 2, 2007, 6:51am UTC](https://www.ruby-forum.com/t/polymorphic-associations-agile-rails-book-confusion/105758 "2007-06-02T06:51:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcampbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@mcampbell](https://www.ruby-forum.com/u/mcampbell)\
**Post date:** [June 2, 2007, 6:51am UTC](https://www.ruby-forum.com/t/polymorphic-associations-agile-rails-book-confusion/105758/1 "2007-06-02T06:51:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mcampbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@mcampbell](https://www.ruby-forum.com/u/mcampbell)\
**Post date:** [June 2, 2007, 10:47am UTC](https://www.ruby-forum.com/t/polymorphic-associations-agile-rails-book-confusion/105758/2 "2007-06-02T10:47:34Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![mcampbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@mcampbell](https://www.ruby-forum.com/u/mcampbell)\
**Post date:** [June 2, 2007, 11:53am UTC](https://www.ruby-forum.com/t/polymorphic-associations-agile-rails-book-confusion/105758/3 "2007-06-02T11:53:33Z")

</div>

Jamal S. 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?

---

<div class="post-metadata">

**Author:** ![mcampbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@mcampbell](https://www.ruby-forum.com/u/mcampbell)\
**Post date:** [June 2, 2007, 12:35pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-agile-rails-book-confusion/105758/4 "2007-06-02T12:35:42Z")

</div>

Michael C. wrote:

> Jamal S. 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.

---

<div class="post-metadata">

**Author:** ![mcampbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@mcampbell](https://www.ruby-forum.com/u/mcampbell)\
**Post date:** [June 2, 2007, 6:02pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-agile-rails-book-confusion/105758/5 "2007-06-02T18:02:11Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mcampbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@mcampbell](https://www.ruby-forum.com/u/mcampbell)\
**Post date:** [June 2, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-agile-rails-book-confusion/105758/6 "2007-06-02T22:56:27Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![mcampbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@mcampbell](https://www.ruby-forum.com/u/mcampbell)\
**Post date:** [June 2, 2007, 6:04pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-agile-rails-book-confusion/105758/7 "2007-06-02T18:04:59Z")

</div>

Understanding Polymorphic Associations  
[http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations](http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations)

You get some examples how it works 🙂

---

<div class="post-metadata">

**Author:** ![mcampbell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8e7dd6/32.png) [@mcampbell](https://www.ruby-forum.com/u/mcampbell)\
**Post date:** [June 3, 2007, 12:16am UTC](https://www.ruby-forum.com/t/polymorphic-associations-agile-rails-book-confusion/105758/8 "2007-06-03T00:16:49Z")

</div>

On 6/2/07, Michael C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 K.

[http://bkocik.net](http://bkocik.net)
