has_many :through
Has Many associations can be configured with the :through option to use an explicit join model to retrieve the data. This operates similarly to a has_and_belongs_to_many association. The advantage is that you’re able to add validations, callbacks, and extra attributes on the join model. Consider the following schema:
class Author < ActiveRecord::Base
has_many :authorships
has_many :books, :through => :authorships
end
class Authorship < ActiveRecord::Base
belongs_to :author
belongs_to :book
end
@author = Author.find :first
@author.authorships.collect { |a| a.book } # selects all books that the author's authorships belong to.
@author.books # selects all books by using the Authorship join model
Sources
- Association Join Models (search for “Association Join Models”)
- Through Associations