HABTM vs Has Many

Hi,

Just wondering what the recommended practice is for creating a
many-to-many relationship between two tables when the join table must
store additional data beyond just the ids of the joined tables.

for example if you have tables, albums and artists and you wanted a
join table with an additional field “year”.

so you’d have schemas:

albums

id
title
num_tracks

artists

name

albums_artists (or something)

album_id
artist_id
release_year

Are there any advantages to representing this in the corresponding
rails models as an habtm relationship between artists and albums vs
pointing both albums and artists at albums_artists using two has_many
relationships?

tia

You are looking for has_many :through relationship. Look up how to do a
has_many :through and you will find all you need. This might make the
relationships clearer:

http://mboffin.com/stuff/ruby-on-rails-data-relationships.png

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs