![](https://bigmachine.io/rails/active_storage/representations/proxy/eyJfcmFpbHMiOnsiZGF0YSI6MTU5LCJwdXIiOiJibG9iX2lkIn19--0c249320d5c0a9cd9dbdae62086377c228ce878b/eyJfcmFpbHMiOnsiZGF0YSI6e30sInB1ciI6InZhcmlhdGlvbiJ9fQ==--743878b0228ee81b4587d6ab790a4e67ea599217/rewrite-hmt.jpg)
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!
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