Skip to content

Instantly share code, notes, and snippets.

@stevepolitodesign
Last active January 28, 2022 21:30
Show Gist options
  • Save stevepolitodesign/87cfcb9c4df3b6158f71e3aa0c680a1d to your computer and use it in GitHub Desktop.
Save stevepolitodesign/87cfcb9c4df3b6158f71e3aa0c680a1d to your computer and use it in GitHub Desktop.
Creating a cascading foreign key when calling references
rails g model session user:references

Before

class CreateSessions < ActiveRecord::Migration[6.1]
  def change
    create_table :sessions do |t|
      t.references :user, null: false, foreign_key: true

      t.timestamps
    end
  end
end

After

class CreateSessions < ActiveRecord::Migration[6.1]
  def change
    create_table :sessions do |t|
      t.references :user, null: false, foreign_key: { on_delete: :cascade }

      t.timestamps
    end
  end
end
@stevepolitodesign
Copy link
Author

stevepolitodesign commented Jan 28, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment