Module: ActiveRecord::ConnectionAdapters::SchemaStatements

Defined in:
lib/forceful_foreign_key.rb

Instance Method Summary collapse

Instance Method Details

#add_foreign_key(from_table, to_table, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/forceful_foreign_key.rb', line 7

def add_foreign_key(from_table, to_table, options = {})
  return unless supports_foreign_keys?
  if options[:force]
    graph = ForcefulForeignKey::Graph.new
    graph.build_graph
    graph.delete_orphans(from_table: from_table,
                         from_column: options[:column] || "#{to_table}_id",
                         target_table: to_table,
                         target_column: options[:primary_key] || 'id')
  end
  old_add_foreign_key from_table, to_table, options
end

#old_add_foreign_keyObject



5
# File 'lib/forceful_foreign_key.rb', line 5

alias_method :old_add_foreign_key, :add_foreign_key