# Table belongs\_to either of two other tables?

**URL:** <https://www.ruby-forum.com/t/table-belongs-to-either-of-two-other-tables/55315>\
**Category:** Rails\
**Created:** [March 19, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/table-belongs-to-either-of-two-other-tables/55315 "2006-03-19T16:46:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 19, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/table-belongs-to-either-of-two-other-tables/55315/1 "2006-03-19T16:46:58Z")

</div>

Suppose I have a site about books and authors, and users can leave  
comments on either a book or an author.

I’d want to have a table called “comments”, which could belong to either  
an author OR a book.

In my structure I wouldn’t be sure whether to include an “author\_id” or  
a “book\_id”, or both.

What would be the best way to handle this relationship?

Thanks in advance for any help,

Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 19, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/table-belongs-to-either-of-two-other-tables/55315/2 "2006-03-19T17:08:06Z")

</div>

look at polymorphic associations in redgerails…

On Sunday, March 19, 2006, at 4:46 PM, Jeff C.man wrote:

> What would be the best way to handle this relationship?  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> Mikkel B.

www.strongside.dk - Football Portal(DK)  
[nflfeed.helenius.org](http://nflfeed.helenius.org) - Football News(DK)  
ting.minline.dk - Buy Old Stuff!(DK)

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 20, 2006, 2:41am UTC](https://www.ruby-forum.com/t/table-belongs-to-either-of-two-other-tables/55315/3 "2006-03-20T02:41:40Z")

</div>

Thanks, that looks like just the thing!

Jeff

Mikkel B. wrote:

> look at polymorphic associations in redgerails…
> 
> On Sunday, March 19, 2006, at 4:46 PM, Jeff C.man wrote:
> 
> > What would be the best way to handle this relationship?  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> > Mikkel B.
> 
> www.strongside.dk - Football Portal(DK)  
> [nflfeed.helenius.org](http://nflfeed.helenius.org) - Football News(DK)  
> ting.minline.dk - Buy Old Stuff!(DK)

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 20, 2006, 10:43am UTC](https://www.ruby-forum.com/t/table-belongs-to-either-of-two-other-tables/55315/4 "2006-03-20T10:43:42Z")

</div>

Am Sonntag, den 19.03.2006, 16:46 +0100 schrieb Jeff C.man:

> Suppose I have a site about books and authors, and users can leave  
> comments on either a book or an author.
> 
> I’d want to have a table called “comments”, which could belong to either  
> an author OR a book.
> 
> In my structure I wouldn’t be sure whether to include an “author\_id” or  
> a “book\_id”, or both.
> 
> What would be the best way to handle this relationship?

class Book \< ActiveRecord::Base  
has\_many :comments  
end

class Author \< ActiveRecord::Base  
has\_many :comments  
end

class Comment \< ActiveRecord::Base  
belongs\_to :book  
belongs\_to :author  
end

In your comments table include a book\_id and an author\_id field.

–  
Norman T.

[http://blog.inlet-media.de](http://blog.inlet-media.de)
