Method: ActiveRecord::ConnectionAdapters::SchemaStatements#remove_reference

Defined in:
activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

#remove_reference(table_name, ref_name, foreign_key: false, polymorphic: false, **options) ⇒ Object Also known as: remove_belongs_to

Removes the reference(s). Also removes a type column if one exists.

Remove the reference
remove_reference(:products, :user, index: false)
Remove polymorphic reference
remove_reference(:products, :supplier, polymorphic: true)
Remove the reference with a foreign key
remove_reference(:products, :user, foreign_key: true)


1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
# File 'activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb', line 1082

def remove_reference(table_name, ref_name, foreign_key: false, polymorphic: false, **options)
  conditional_options = options.slice(:if_exists, :if_not_exists)

  if foreign_key
    reference_name = Base.pluralize_table_names ? ref_name.to_s.pluralize : ref_name
    if foreign_key.is_a?(Hash)
      foreign_key_options = foreign_key.merge(conditional_options)
    else
      foreign_key_options = { to_table: reference_name, **conditional_options }
    end
    foreign_key_options[:column] ||= "#{ref_name}_id"
    remove_foreign_key(table_name, **foreign_key_options)
  end

  remove_column(table_name, "#{ref_name}_id", **conditional_options)
  remove_column(table_name, "#{ref_name}_type", **conditional_options) if polymorphic
end