# Associating a resource with multiple resources?

**URL:** <https://discuss.rubyonrails.org/t/associating-a-resource-with-multiple-resources/26732>\
**Category:** rubyonrails-talk\
**Created:** [March 23, 2008, 2:17pm UTC](https://discuss.rubyonrails.org/t/associating-a-resource-with-multiple-resources/26732 "2008-03-23T14:17:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Houman\_Dunnil](https://avatars.discourse-cdn.com/v4/letter/h/da6949/32.png) [@Houman\_Dunnil](https://discuss.rubyonrails.org/u/Houman_Dunnil)\
**Post date:** [March 23, 2008, 2:17pm UTC](https://discuss.rubyonrails.org/t/associating-a-resource-with-multiple-resources/26732/1 "2008-03-23T14:17:37Z")

</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:** ![AndyV1](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@AndyV1](https://discuss.rubyonrails.org/u/AndyV1)\
**Post date:** [March 24, 2008, 7:32pm UTC](https://discuss.rubyonrails.org/t/associating-a-resource-with-multiple-resources/26732/2 "2008-03-24T19:32:56Z")

</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| &nbsp;&nbsp;t.reference :publishable, :polymorphic=\>true &nbsp;&nbsp;t.reference :author end

class Publishing \< ARec::Base &nbsp;&nbsp;belongs\_to :author &nbsp;&nbsp;belongs\_to :publishable, :polymorphic=\>true &nbsp;&nbsp;... end

class Author \< ARec::Base &nbsp;&nbsp;has\_many :publishings &nbsp;&nbsp;... end

class Book \< ARec::Base &nbsp;&nbsp;has\_many :publishings, :as=\>:publishable &nbsp;&nbsp;has\_many :authors, :through=\>:publishing &nbsp;&nbsp;... end

class Article \< ARec::Base &nbsp;&nbsp;has\_many :publishings, :as=\>:publishable &nbsp;&nbsp;has\_many :authors, :through=\>:publishing &nbsp;&nbsp;... 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.
