method
add_reference
v4.2.7 -
Show latest stable
- Class:
ActiveRecord::ConnectionAdapters::SchemaStatements
add_reference(table_name, ref_name, options = {})public
Adds a reference. The reference column is an integer by default, the :type option can be used to specify a different type. Optionally adds a _type column, if :polymorphic option is provided. add_reference and add_belongs_to are acceptable.
The options hash can include the following keys:
- :type
-
The reference column type. Defaults to :integer.
- :index
-
Add an appropriate index. Defaults to false.
- :foreign_key
-
Add an appropriate foreign key. Defaults to false.
- :polymorphic
-
Wether an additional _type column should be added. Defaults to false.
Create a user_id integer column
add_reference(:products, :user)
Create a user_id string column
add_reference(:products, :user, type: :string)
Create supplier_id, supplier_type columns and appropriate index
add_reference(:products, :supplier, polymorphic: true, index: true)
1Note
foreign_key option
It is also possible to specify that +add_foreign_key+ should be added to the schema: add_reference(:products, :supplier, foreign_key: true)