Extending DHH''s example of a through association somewhat:
class Author
has_many :authorships
has_many :books,
:class_name => ''Textbook'', # <-- do not
include
:through => :authorships,
:include => :chapters # <-- inoperative
end
class Textbook
has_many :authorships
has_many :authors, :through => :authorships
has_many :chapters
end
class Authorship
belongs_to :author
belongs_to :book, :class_name => ''Textbook''
end
class Chapter < ActiveRecord::Base
belongs_to :textbook
end
1. In the through association documentation it should be noted
that the :class_name => ''Textbook'' option should not be
included
in the through association declaration itself, otherwise
Rails looks for a :textbook association in Authorship.
2. Through associations do not currently support the :include
option, either in their declarations or through an :include
on the source belongs_to declaration.
author.books.find(:all, :include => :chapters) does however work,
so it should be easy to get a default include working.
Mark