Skip to content

Instantly share code, notes, and snippets.

@bmxpert1
Last active December 25, 2015 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmxpert1/6967951 to your computer and use it in GitHub Desktop.
Save bmxpert1/6967951 to your computer and use it in GitHub Desktop.
Foreigner methods to create and remove foreign keys for cross schema use in PostgreSQL
ActiveRecord::Migration.class_eval do
def add_foreign_key_ignore_prefix(from_table, to_table, options = {})
table_name_prefix = ActiveRecord::Base.table_name_prefix
ActiveRecord::Base.table_name_prefix = ''
add_foreign_key(from_table, to_table, options)
ActiveRecord::Base.table_name_prefix = table_name_prefix
end
def remove_foreign_key(from_table, options = {})
table_name_prefix = ActiveRecord::Base.table_name_prefix
super(from_table.to_s.gsub(/#{table_name_prefix}/, ''), options)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment