Arman H
Arman H

Reputation: 5618

How to get value from a joiner table in Sequel?

I have a joiner table that connects 2 tables, and stores some additional meta-information about the relationship. How can I get values from the joiner table, using either dataset or model methods?

For example:

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 contains an extra field, authorship, which signifies whether an artist was the primary author of the album, a collaborator, a guest appearance, etc:

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

I'd like to get the artist's authorship value for a particular album:

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

# authorship = ???

Upvotes: 0

Views: 66

Answers (1)

Jeremy Evans
Jeremy Evans

Reputation: 12139

The default in Sequel is to only select from columns in the associated table, not columns in the join table. You can use the :select option to specify a selection: Artist.many_to_many :albums, :select=>[Sequel.expr(:albums).*, :authorship]

Upvotes: 1

Related Questions