Convertig a HABTM join table to a real activerecord model

If your has_and_belongs_to_many associations suddenly need to know more, its time to convert them to a real model.

  • rename table
  • add id
  • add created_at/updated_at as not-null (need to set existing records to something)


class CreateJoinModel < ActiveRecord::Migration
  def change
    rename_table :products_users, :purchases
    add_column :purchases, :id, :primary_key
    [:created_at, :updated_at].each do |column|
      add_column :purchases, column, :timestamp, null: false, default:
      change_column_default :purchases, column, nil

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s