instance method
create_join_table
Ruby on Rails 4.2.9
Since v4.0.13Signature
create_join_table(table_1, table_2, options = {})
Creates a new join table with the name created using the lexical order of the first two arguments. These arguments can be a String or a Symbol.
# Creates a table called 'assemblies_parts' with no id. create_join_table(:assemblies, :parts)
You can pass a options hash can include the following keys:
:table_name-
Sets the table name overriding the default
:column_options-
Any extra options you want appended to the columns definition.
:options-
Any extra options you want appended to the table definition.
:temporary-
Make a temporary table.
:force-
Set to true to drop the table before creating it. Defaults to false.
Note that create_join_table does not create any indices by default; you can use its block form to do so yourself:
create_join_table :products, :categories do |t| t.index :product_id t.index :category_id end
Add a backend specific option to the generated SQL (MySQL)
create_join_table(:assemblies, :parts, options: 'ENGINE=InnoDB DEFAULT CHARSET=utf8')
generates:
CREATE TABLE assemblies_parts ( assembly_id int NOT NULL, part_id int NOT NULL, ) ENGINE=InnoDB DEFAULT CHARSET=utf8
Parameters
-
table_1req -
table_2req -
optionsopt = {}
Source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 275
def create_join_table(table_1, table_2, options = {})
join_table_name = find_join_table_name(table_1, table_2, options)
column_options = options.delete(:column_options) || {}
column_options.reverse_merge!(null: false)
t1_column, t2_column = [table_1, table_2].map{ |t| t.to_s.singularize.foreign_key }
create_table(join_table_name, options.merge!(id: false)) do |td|
td.integer t1_column, column_options
td.integer t2_column, column_options
yield td if block_given?
end
end
Defined in activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb line 275
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::ConnectionAdapters::SchemaStatements