To define a foreign key to a table named differently than the column name, use to_table:
instead of foreign_key: true
. Note that to_table
is the name of the database table, which needs to include a table prefix, if one is defined.
add_reference :cats, :parent, null: true, foreign_key: { to_table: :cats }