ActiveRecord 多态关联:使 ID 变长而不是整数?

ActiveRecord polymorphic association: making ID long instead of integer?

这似乎是定义多态关联的唯一方法:

t.references :poly_column, :polymorphic => true

如何使结果 poly_column_id 使用 long 类型而不是 integer (默认)?

查看 RailsGuides. Our first question should be, what does t.references :poly_column, :polymorphic => true actually do? Based on the example in the guide, we might expect it to be a shortcut for t.integer :imageable_id and t.string :imageable_type, which we can also confirm in the RailsAPI。因此,只需自己定义它们:

t.long :poly_column_id
t.string :poly_column_type

您可以 change_column 在创建 table 之后明确使用您需要的任何内容。