# Associating a resource with multiple resources?

**URL:** <https://www.ruby-forum.com/t/associating-a-resource-with-multiple-resources/132688>\
**Category:** Rails\
**Created:** [March 23, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/associating-a-resource-with-multiple-resources/132688 "2008-03-23T15:18:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Houman\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Houman\_D](https://www.ruby-forum.com/u/Houman_D)\
**Post date:** [March 23, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/associating-a-resource-with-multiple-resources/132688/1 "2008-03-23T15:18:54Z")

</div>

Hi,

I have these models in my ror application: author, article and book. I  
want to associate author to article and book so:

book has many authors  
article has many authors  
author belongs to both of them

What’s the best practice for doing so? Should I have both book\_id and  
article\_id in author table? Other solutions?

Thanks in advance,

---

<div class="post-metadata">

**Author:** ![Houman\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Houman\_D](https://www.ruby-forum.com/u/Houman_D)\
**Post date:** [March 24, 2008, 3:39am UTC](https://www.ruby-forum.com/t/associating-a-resource-with-multiple-resources/132688/2 "2008-03-24T03:39:54Z")

</div>

Houman,

I think you want a has\_many :through relationship. Here is an analogy:

[http://wiki.rubyonrails.org/rails/pages/Beginner+Howto+on+has\_many+:through](http://wiki.rubyonrails.org/rails/pages/Beginner+Howto+on+has_many+:through)

Arshak

Houman D. wrote:

> Hi,
> 
> I have these models in my ror application: author, article and book. I  
> want to associate author to article and book so:
> 
> book has many authors  
> article has many authors  
> author belongs to both of them
> 
> What’s the best practice for doing so? Should I have both book\_id and  
> article\_id in author table? Other solutions?
> 
> Thanks in advance,

---

<div class="post-metadata">

**Author:** ![Houman\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/cdc98d/32.png) [@Houman\_D](https://www.ruby-forum.com/u/Houman_D)\
**Post date:** [March 24, 2008, 8:33pm UTC](https://www.ruby-forum.com/t/associating-a-resource-with-multiple-resources/132688/3 "2008-03-24T20:33:58Z")

</div>

If you have reasons to stick with the current model, then introduce a  
fourth concept, call it ‘Publishing’. A ‘publishing’ is something  
that an author has written. In this case, it associates an author to  
a book or an article. Now you can use two techniques –  
has\_many :through (as suggested by Arshak) and polymorphic  
associations.

create\_table :publishing do |t|  
t.reference :publishable, :polymorphic=\>true  
t.reference :author  
end

class Publishing \< ARec::Base  
belongs\_to :author  
belongs\_to :publishable, :polymorphic=\>true  
…  
end

class Author \< ARec::Base  
has\_many :publishings  
…  
end

class Book \< ARec::Base  
has\_many :publishings, :as=\>:publishable  
has\_many :authors, :through=\>:publishing  
…  
end

class Article \< ARec::Base  
has\_many :publishings, :as=\>:publishable  
has\_many :authors, :through=\>:publishing  
…  
end

The other possibility would be to use Single Table Inheritance,  
putting the books and articles into a single table that uses a  
discriminator column so that you can tell them apart. You can still  
use the has\_many :through concept (to support 1 or more authors) but  
you eliminate one table.
