Permalink
Browse files

Add a better explanation in the many_through_many documentation

  • Loading branch information...
1 parent cc3cce9 commit 7149ef195095d904891e5ca31fb3f9ad63aa10b1 @jeremyevans committed Dec 17, 2012
Showing with 23 additions and 0 deletions.
  1. +23 −0 lib/sequel/plugins/many_through_many.rb
@@ -13,6 +13,29 @@ module Plugins
#
# Which will give you the tags for all of the artist's albums.
#
+ # Let's break down the 2nd argument of the many_through_many call:
+ #
+ # [[:albums_artists, :artist_id, :album_id],
+ # [:albums, :id, :id],
+ # [:albums_tags, :album_id, :tag_id]]
+ #
+ # This argument is an array of arrays with three elements. Each entry in the main array represents a JOIN in SQL:
+ #
+ # * The first element in each array represents the name of the table to join.
+ # * The second element in each array represents the column used to join to the previous table.
+ # * The third element in each array represents the column used to join to the next table.
+ #
+ # So the "Artist.many_through_many :tags" is translated into something similar to:
+ #
+ # FROM artists
+ # JOIN albums_artists ON (artists.id = albums_artists.artist_id)
+ # JOIN albums ON (albums_artists.album_id = albums.id)
+ # JOIN albums_tags ON (albums.id = albums_tag.album_id)
+ # JOIN tags ON (albums_tags.tag_id = tags.id)
+ #
+ # The "artists.id" and "tags.id" criteria come from other association options (defaulting to the primary keys of the current and
+ # associated tables), but hopefully you can see how each argument in the array is used in the JOIN clauses.
+ #
# Here are some more examples:
#
# # Same as Artist.many_to_many :albums

0 comments on commit 7149ef1

Please sign in to comment.