instance method remove_reference

Ruby on Rails 7.2.3

Since v4.0.13

Available in: v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

remove_reference(table_name, ref_name, foreign_key: false, polymorphic: false, **options)

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)

Parameters

table_name req
ref_name req
foreign_key key = false
polymorphic key = false
options keyrest
Source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 1064
      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

Defined in activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb line 1064 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::ConnectionAdapters::SchemaStatements

Type at least 2 characters to search.

↑↓ navigate · open · esc close