I like has_many through with a CatalogEntry join model.
example Page call
Page.last.chapter.book.subjects.first
models
class CatalogEntry < ActiveRecord::Base
belongs_to :subject
belongs_to :book
end
class Book < ActiveRecord::Base
has_many :catalog_entries
has_many :subjects, :through => :catalog_entries
has_many :chapters
end
class Subject < ActiveRecord::Base
has_many :catalog_entries
has_many :books, :through => :catalog_entries
end
class Chapter < ActiveRecord::Base
belongs_to :book
has_many :pages
end
class Page < ActiveRecord::Base
belongs_to :chapter
end
seeds.rb
subject01 = Subject.create!(:name => ‘Subject One’)
subject02 = Subject.create!(:name => ‘Subject Two’)
book01 = Book.create!(:name => ‘Book One’)
book02 = Book.create!(:name => ‘Book Two’)
book01_chapter01 = Chapter.create!(:book => book01, :name => ‘Chapter
One’)
book01_chapter02 = Chapter.create!(:book => book01, :name => ‘Chapter
Two’)
book02_chapter01 = Chapter.create!(:book => book02, :name => ‘Chapter
One’)
book02_chapter02 = Chapter.create!(:book => book02, :name => ‘Chapter
Two’)
book01_chapter01_page01 = Page.create!(:chapter => book01_chapter01,
:number => 1)
book01_chapter02_page02 = Page.create!(:chapter => book01_chapter02,
:number => 2)
book02_chapter01_page01 = Page.create!(:chapter => book02_chapter01,
:number => 1)
book02_chapter02_page02 = Page.create!(:chapter => book02_chapter02,
:number => 2)
book01.subjects << subject01
book01.subjects << subject02
book01.save
book02.subjects << subject01
book02.subjects << subject02
book02.save