1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
Ernst Rullmann 2016-01-31 22:50:20 -05:00
parent 2f8ba24ec6
commit 138c1db83e

View file

@ -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