Created
November 4, 2009 23:01
-
-
Save TMorgan99/226468 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'config/environment' | |
puts <<-__MIGRATE | |
ActiveRecord::Schema.define do | |
__MIGRATE | |
join_tables = Hash.new | |
Dir.glob( './app/models/**/*' ).map { |f| # file name | |
f =~ /(\w+).rb/ | |
Object.const_get $1.classify rescue nil | |
}.compact.map { |m| # model class | |
m.reflect_on_all_associations | |
}.flatten.select { |a| # association | |
a.options.keys.include? :join_table | |
}.each do |c| | |
table = c.options[ :join_table ] | |
# both sides of the association will appear. we only want the first one | |
puts <<-__MIGRATE unless join_tables.has_key? table | |
create_table "#{ table }", :id => false, :force => true do |t| | |
t.integer "#{ c.primary_key_name }" | |
t.integer "#{ c.association_foreign_key }" | |
end | |
__MIGRATE | |
join_tables[table] = true | |
end | |
puts <<-__MIGRATE | |
end | |
__MIGRATE | |
## Now, tack this generated code into the up method of a migration ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment