From 138c1db83e13cc01c54ac9657c8388aa46324582 Mon Sep 17 00:00:00 2001 From: Ernst Rullmann Date: Sun, 31 Jan 2016 22:50:20 -0500 Subject: Added references option to join tables Fixes issue #22960 When creating join tables with the command rails g migration CreateJoinTableShowroomUser showroom:references user:references The migration will use references to create the joins and output: class CreateJoinTableShowroomUser < ActiveRecord::Migration def change create_join_table :showrooms, :users do |t| t.references :showroom, index: true, foreign_key: true t.references :user, index: true, foreign_key: true end end end This allows for proper refrences with indexes and foreign keys to be easily used when adding join tables. Without `:refrences` the normal output is generated: class CreateJoinTableShowroomUser < ActiveRecord::Migration[5.0] def change create_join_table :showrooms, :users do |t| # t.index [:showroom_id, :user_id] # t.index [:user_id, :showroom_id] end end end --- .../rails/generators/active_record/migration/templates/migration.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activerecord/lib/rails/generators/active_record/migration/templates') diff --git a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb index 107f107dc4..481c70201b 100644 --- a/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb +++ b/activerecord/lib/rails/generators/active_record/migration/templates/migration.rb @@ -19,7 +19,11 @@ class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Mi def change create_join_table :<%= join_tables.first %>, :<%= join_tables.second %> do |t| <%- attributes.each do |attribute| -%> + <%- if attribute.reference? -%> + t.references :<%= attribute.name %><%= attribute.inject_options %> + <%- else -%> <%= '# ' unless attribute.has_index? -%>t.index <%= attribute.index_name %><%= attribute.inject_index_options %> + <%- end -%> <%- end -%> end end -- cgit v1.2.3