Associating a resource with multiple resources?

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,

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.