diff options
author | Ernst Rullmann <e.rullmann@gmail.com> | 2016-01-31 22:50:20 -0500 |
---|---|---|
committer | Ernst Rullmann <e.rullmann@gmail.com> | 2016-01-31 23:20:07 -0500 |
commit | 138c1db83e13cc01c54ac9657c8388aa46324582 (patch) | |
tree | face3127cde08566746fcf5d47f7620e5f3f9d97 /activerecord/lib/rails | |
parent | 2f8ba24ec65d7a850e7ec0985d44be4a86da04c5 (diff) | |
download | rails-138c1db83e13cc01c54ac9657c8388aa46324582.tar.gz rails-138c1db83e13cc01c54ac9657c8388aa46324582.tar.bz2 rails-138c1db83e13cc01c54ac9657c8388aa46324582.zip |
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
Diffstat (limited to 'activerecord/lib/rails')
-rw-r--r-- | activerecord/lib/rails/generators/active_record/migration/templates/migration.rb | 4 |
1 files changed, 4 insertions, 0 deletions
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 |