Module: AutomaticForeignKey::ActiveRecord::Migration::ClassMethods

Defined in:
lib/automatic_foreign_key/active_record/migration.rb

Instance Method Summary collapse

Instance Method Details

#add_column(table_name, column_name, type, options = {}) ⇒ Object

Overrides ActiveRecord#add_column and adds foreign key if column references other column

add_column(‘comments’, ‘post_id’, :integer)

# creates a column and adds foreign key on posts(id)

add_column(‘comments’, ‘post_id’, :integer, :on_update => :cascade, :on_delete => :cascade)

# creates a column and adds foreign key on posts(id) with cascade actions on update and on delete

add_column(‘comments’, ‘post_id’, :integer, :index => true)

# creates a column and adds foreign key on posts(id)
# additionally adds index on posts(id)

add_column(‘comments’, ‘post_id’, :integer, :index => { :unique => true, :name => ‘comments_post_id_unique_index’ }))

# creates a column and adds foreign key on posts(id)
# additionally adds unique index on posts(id) named comments_post_id_unique_index

add_column(‘addresses’, ‘citizen_id’, :integer, :references => :users

# creates a column and adds foreign key on users(id)

add_column(‘addresses’, ‘citizen_id’, :integer, :references => [:users, :uuid]

# creates a column and adds foreign key on users(uuid)


30
31
32
33
# File 'lib/automatic_foreign_key/active_record/migration.rb', line 30

def add_column(table_name, column_name, type, options = {})
  super
  handle_column_options(table_name, column_name, options)
end

#change_column(table_name, column_name, type, options = {}) ⇒ Object



35
36
37
38
39
# File 'lib/automatic_foreign_key/active_record/migration.rb', line 35

def change_column(table_name, column_name, type, options = {})
  super
  remove_foreign_key_if_exists(table_name, column_name)
  handle_column_options(table_name, column_name, options)
end