views:

16

answers:

0

I hope this is a simple problem for someone because I am going round and round in circles

I have an Author who can author/compose or co-author, either Songs and or Tunes. A Song constitutes the lyrics, title etc. The Tune is the score and mp3 url.

I have tried the following without success

class Author < ActiveRecord::Base
  has_many :works
  has_many :songs, :through => :works, :source => 'Song'
  has_many :tunes, :through => :works, :source => 'Tune'
end

class Work < ActiveRecord::Base
  belongs_to :authored_work, :polymorphic => true
  belongs_to :author
end

class Song < ActiveRecord::Base
  has_many :works, :as => :authored_work
  has_many :authors, :through => :works
  belongs_to :tune
end

class Tune < ActiveRecord::Base
  has_many :works, :as => :authored_work
  has_many :composers, :through => :works, :class_name => 'Author'
end

At the least I need a way to get

Author.songs
Author.tunes
@tune.composers
@hymn.authors

I'm not even sure how I would assign a song to an author with this polymorphic setup. It was originally designed with a many-to-one (authors-to-song/tune) but now they want multiple authors for each. Yes, it could be done with two tables (author_song, composer_tune) but it seems redundant to duplicate essentially author_id, authorable_id, authorable_type.

I am half way there if I define my own hymns/tunes methods on Author to get them and create the association with @tune.composers << @composer

Regards