Convert a Rails association from “has and belongs to many” to “has many through”

There are a lot of blog posts that warn you about not using the Rails has_and_belongs_to_many association since you can’t predict when you will need additional functionality on the join model.

I did it anyways. Of course I ended up having to convert to a has_many :through association.

Here’s how I did it.

Original models

Update the associations

Go ahead and change the association on the models right away.

Updating models up front means that the migrations you will write shortly have to work with the updated associations. If you don’t do that you might have problems running the migrations in production.

Create a new join table

The automatic table join was called teams_users and doesn’t have an id.

Create a new table with an id and the belongs_to fields.

Pick a temporary name for the table.

Migrate the data

Here’s the important bit: copy all the rows from the old join table to the new one.

Do this by using a local version of the ActiveRecord models with both associations in it.

Local AR classes have both the has and belongs to many association and the has many association

Note that it’s not necessary to have the 2 associations in both the User and Team models, but it doesn’t hurt and I find it helped me write the correct migration code in the up method on the first try.

Drop the old join table

Or keep the old join table for a while until you are sure no data was lost

Rename the new table

Next time I know that I’ll be using a has many through association from the start!

--

--

--

CTO at MuniRent. Sharing my love of code

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Julien Vanier

Julien Vanier

CTO at MuniRent. Sharing my love of code

More from Medium

Preparing for Heaven’s Door

To go in the direction of the water

Will There Be ‘Flying Cars’ in the Next Seven Years?

Modern Japanese Imperialism