From a03ea684efc3505647cf0327a501aa2dbb591ad2 Mon Sep 17 00:00:00 2001
From: Sean Griffin <sean@thoughtbot.com>
Date: Mon, 22 Dec 2014 13:36:20 -0700
Subject: Use the new `foreign_key` option on `references` in generators

Changes `rails g model Post user:references` from

    def change
      create_table :posts do |t|
        t.references :user, index: true
      end

      add_foreign_key :posts, :users
    end

to

    def change
      create_table :posts do |t|
        t.references :user, index: true, foreign_key: true
      end
    end

Changes `rails g migration add_user_to_posts user:references` from

    def change
      add_reference :posts, :users, index: true
      add_foreign_key :posts, :users
    end

to

    def change
      add_reference :posts, :users, index: true, foreign_key: true
    end
---
 .../active_record/migration/templates/create_table_migration.rb     | 3 ---
 .../rails/generators/active_record/migration/templates/migration.rb | 6 ------
 2 files changed, 9 deletions(-)

(limited to 'activerecord')

diff --git a/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb b/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb
index fb0fbb4759..f7bf6987c4 100644
--- a/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb
+++ b/activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb
@@ -14,9 +14,6 @@ class <%= migration_class_name %> < ActiveRecord::Migration
     end
 <% attributes_with_index.each do |attribute| -%>
     add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
-<% end -%>
-<% attributes.select(&:reference?).reject(&:polymorphic?).each do |attribute| -%>
-    add_foreign_key :<%= table_name %>, :<%= attribute.name.pluralize %>
 <% end -%>
   end
 end
diff --git a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb
index 7df9bcad25..ae9c74fd05 100644
--- a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb
+++ b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb
@@ -4,9 +4,6 @@ class <%= migration_class_name %> < ActiveRecord::Migration
 <% attributes.each do |attribute| -%>
   <%- if attribute.reference? -%>
     add_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %>
-    <%- unless attribute.polymorphic? -%>
-    add_foreign_key :<%= table_name %>, :<%= attribute.name.pluralize %>
-    <%- end -%>
   <%- else -%>
     add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
     <%- if attribute.has_index? -%>
@@ -29,9 +26,6 @@ class <%= migration_class_name %> < ActiveRecord::Migration
 <%- if migration_action -%>
   <%- if attribute.reference? -%>
     remove_reference :<%= table_name %>, :<%= attribute.name %><%= attribute.inject_options %>
-    <%- unless attribute.polymorphic? -%>
-    remove_foreign_key :<%= table_name %>, :<%= attribute.name.pluralize %>
-    <%- end -%>
   <%- else -%>
     <%- if attribute.has_index? -%>
     remove_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
-- 
cgit v1.2.3