For Rails 5+
Initial Definition:
If you are defining your Post
model table, you can set references
, index
and foreign_key
in one line:
t.references :author, index: true, foreign_key: { to_table: :users }
Update Existing:
If you are adding references to an existing table, you can do this:
add_reference :posts, :author, foreign_key: { to_table: :users }
Note: The default value for index
is true.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…