More Complex Many to Many

Buy or Subscribe

You can access this course in just a minute, and support my efforts to rid the world of crappy online courses!

Buy Standalone  Subscribe

I didn't pick the best example for this video (artists and tracks), but if you imagine that songs can be covered by many artists (think "Mack the Knife" or something), it works:
class Artist < ApplicationRecord
  has_many :albums
  has_many :tracks, through: :albums
end
The other end of the association needs to link back:
class Track < ApplicationRecord
  belongs_to :album
  belongs_to :media_type
  belongs_to :genre
  has_and_belongs_to_many :playlists
  has_many :artists, through: :album
end