如何从 Sequel 中的加入者 table 获取价值?

How to get value from a joiner table in Sequel?

我有一个连接器 table 连接 2 tables,并存储一些关于关系的额外元信息。如何使用 datasetmodel 方法从连接器 table 获取值?

例如:

class Artist < Sequel::Model
    many_to_many :albums,
    left_key: :artist_id,
    right_key: :albums_id,
    join_table: :artists_albums
end

class Albums < Sequel::Model
    many_to_many :albums,
    left_key: :album_id,
    right_key: :artist_id,
    join_table: :artists_albums
end

Joiner table 包含一个额外的字段,authorship,表示艺术家是否是专辑的主要作者、合作者、客串等:

CREATE TABLE `artists_albums` (`artist_id` integer, `album_id` integer, `authorship` varchar(255))

我想获取特定专辑的艺术家 authorship 值:

artist = Artist[1]
album = artist.albums.first

# authorship = ???

Sequel 中的默认值仅 select 来自关联 table 中的列,而不是连接 table 中的列。您可以使用 :select 选项指定 select 离子:Artist.many_to_many :albums, :select=>[Sequel.expr(:albums).*, :authorship]