Jeremy, thanks for your quick answer. Much obliged.

Reading the documentation for the first time, I ran into another small 
hiccup.
It wasn't clear to me, that the first parameter to ›many_through_many‹ is 
used to infer the ‘last’ join.

I want to give a small example; *id* denotes the primary key. foo[bar]denotes a 
foreign_key foo on the primary key of bar. 

artists(*id*, name)
DB.create_table(:artists) {
  primary_key :id,
  String :name
}

albums(*id*, name)
tags(*id*, name)

albums_artists(*artist_id[artists], album_id[albums]*)
DB.create_join_table({:artist_id => :artists, :album_id => :albums}) # name 
is albums_artists *instead* of artists_albums !

albums_tags(*album_id[albums], tag_id[tags]*)

So we have 2 many_to_many relationships here:
artist -- n x m -- albums -- n x o -- tags


class Artist < Sequel::Model
  many_to_many :albums
end

class Albums < Sequel::Model
  many_to_many :artists
  many_to_many :tags
end
class Tag < Sequel::Model
  many_to_many :albums
end

So far so good. 
To issue an Artist.first.tags this would be an appropriate 
many_through_manycall for Artists
class Artists < Sequel::Model
   many_through_many :tags, [[:albums_artists, :artist_id, :albums_id], # 
from artists join albums_artists on artist.id = artists_albums.artist_id
                            [:albums_tags, :album_id, :tag_id]] # join 
albums_tags on albums_artists.album_id = albums_tags.tag_id
end

This automagically infers  join tags on albums_tags.tag_id = tags.id via 
:tags → Tag (Sequel::Model) → :tags (Sequel::Dataset)
*As I didn't know that, I added *[:tags, :tag_id, :id] to force the join of 
albums_tags with tags.
But that issued another join. A selfjoin of tags: join … on tags as t1 
where tags.id = t1.id

My point is: Perhaps you can update the documentation to stress this fact 
and describe the name inference mechanism a little bit.
I used a table named :beamcount_eqlass and had to use many_through_many 
:beamcount_eqlass*es*.

Perhaps this can be helpful to explain the crossed relations from another 
viewpoint.
class A < Sequel::Model
  many_through_many :D, [[:AB, :AB_Akey, :AB_BCkey],
                         [:BC, :BC_ABkey, :BP_Dkey]]
end
A join AB on A.Pkey = AB_Akey
join BC on AB.BCkey = BC.ABkey
join D on BC.Dkey = D.Pkey


And furthermore it's possible to use a complete different name and pass a 
:class parameter

class Artist < Sequel::Model
  many_to_many :albums
  many_through_many *:schlagworte*, [[:albums_artists, :artist_id, 
:album_id],
                       [:albums_tags, :album_id, :tag_id]],
                    :distinct => true, *:class => :Tag*
end

Cheers, Johannes

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Attachment: artist_album_tag.rb
Description: Binary data

Reply via email to