Best Models Associations suggestions

Hello All I am working on my new project. in that my requriments are like this

Subjects have many books, book belongs to subjects Books have many Chapters, Chapter belongs to Books Chapter have many pages, page belongs to Chapters Is that fine or it could be done better Ways

Thanks in advance

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

Devil’s advocate here… what do you gain in this example by using a has_many :through instead of has_and_belongs_to_many? Is one ever really going to neeed to access a CatalogEntry instance?

There are two things I like about has_many :through. First, it causes me to think about and try to name the relationship. Second, it treats the join model as a first class entity, formalizing the relationship - so i can say book.catalog_entries as well as book.subjects.

Form me, has_and_belongs_to_many obscures rather than reveals

relationships so avoided it.

My preference does not define general practice. So let's look at one

authority. From The Rails 3 Way, section 7.5.1.

        Before

proceeding with this section, I must clear my conscience by stating that has_and_belongs_to_many is practically obsolete in the minds of many Rails developers, including the authors of this book. Use has_many :through instead and your life should be a lot easier. The section is preserved in this edition almost exactly as it appeared in the first, because it contains good techniques that enlighten the reader about nuances of Active Record behavior.

That, too, is an opinion. But one probably more broadly founded than

mine.

As to a 'need' for CatalogEntry.... You need a join model. HABTM

uses a join model. It just elides the fact.

Is the devil's advocate satisfied?