Skip to content

Generate database foreign keys based on ActiveRecord metadata (:belongs_to).

License

Notifications You must be signed in to change notification settings

bretweinraub/fk_generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FkGenerator
===========


Usage 
=====

script/generate fk_generator TrainingSessionAssoc app/models/training_session_assoc.rb 


Generates:
==========

class TrainingSessionAssoc < ActiveRecord::Migration
  def self.up
      execute "ALTER TABLE training_session_assocs ADD CONSTRAINT training_session_assocs_fk_trainings_training_id FOREIGN KEY (training_id) REFERENCES trainings(id)"
      execute "ALTER TABLE training_session_assocs ADD CONSTRAINT training_session_assocs_fk_training_sessions_training_session_id FOREIGN KEY (training_session_id) REFERENCES training_sessions(id)"
  end

  def self.down
      execute "alter table training_session_assocs DROP CONSTRAINT training_session_assocs_fk_trainings_training_id"
      execute "alter table training_session_assocs DROP CONSTRAINT training_session_assocs_fk_training_sessions_training_session_id"
    end
end


Original Model
==============


class TrainingSessionAssoc < ActiveRecord::Base

  # hobo_model # Don't put anything above this

  fields do
    timestamps
  end

  belongs_to :training
  belongs_to :training_session

  index [:training_id,:training_session_id], {:unique => true, :name => "training_session_assoc_uk1"}

end

About

Generate database foreign keys based on ActiveRecord metadata (:belongs_to).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages