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.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