method

add_reference

add_reference(table_name, ref_name, options = {})
public

Adds a reference. Optionally adds a type column, if :polymorphic option is provided. The reference column is an integer by default, the :type option can be used to specify a different type. add_reference and add_belongs_to are acceptable.

Create a user_id integer column
add_reference(:products, :user)
Create a user_id string column
add_reference(:products, :user, type: :string)
Create a supplier_id and supplier_type columns
add_belongs_to(:products, :supplier, polymorphic: true)
Create supplier_id, supplier_type columns and appropriate index
add_reference(:products, :supplier, polymorphic: true, index: true)

1Note

foreign_key option

cmbankester ยท Jun 7, 2016

It is also possible to specify that +add_foreign_key+ should be added to the schema: add_reference(:products, :supplier, foreign_key: true)