Module Sequel::Plugins::ManyThroughMany
In: lib/sequel/plugins/many_through_many.rb

The many_through_many plugin allow you to create a association to multiple objects using multiple join tables. For example, assume the following associations:

   Artist.many_to_many :albums
   Album.many_to_many :tags

The many_through_many plugin would allow this:

   Artist.plugin :many_through_many
   Artist.many_through_many :tags, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_tags, :album_id, :tag_id]]

Which will give you the tags for all of the artist‘s albums.

Here are some more examples:

  # Same as Artist.many_to_many :albums
  Artist.many_through_many :albums, [[:albums_artists, :artist_id, :album_id]]

  # All artists that are associated to any album that this artist is associated to
  Artist.many_through_many :artists, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_artists, :album_id, :artist_id]]

  # All albums by artists that are associated to any album that this artist is associated to
  Artist.many_through_many :artist_albums, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id],      #    [:albums_artists, :album_id, :artist_id], [:artists, :id, :id], [:albums_artists, :artist_id, :album_id]],      #    :class=>:Album

  # All tracks on albums by this artist
  Artist.many_through_many :tracks, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id]],      #    :right_primary_key=>:album_id

Often you don‘t want the current object to appear in the array of associated objects. This is easiest to handle via an :after_load hook:

  Artist.many_through_many :artists, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_artists, :album_id, :artist_id]],
    :after_load=>proc{|artist, associated_artists| associated_artists.delete(artist)}

You can also handle it by adding a dataset block that excludes the current record (so it won‘t be retrieved at all), but that won‘t work when eagerly loading, which is why the :after_load proc is recommended instead.

It‘s also common to not want duplicate records, in which case the :distinct option can be used:

  Artist.many_through_many :artists, [[:albums_artists, :artist_id, :album_id], [:albums, :id, :id], [:albums_artists, :album_id, :artist_id]],
   :distinct=>true

Classes and Modules

Module Sequel::Plugins::ManyThroughMany::ClassMethods
Class Sequel::Plugins::ManyThroughMany::ManyThroughManyAssociationReflection

[Validate]