Rails:'references:foo'和'integer:foo_id'之间有区别吗? [英] Rails: is there a difference between 'references :foo' and 'integer :foo_id'?

查看:132
本文介绍了Rails:'references:foo'和'integer:foo_id'之间有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在迁移中使用references :foo时,生成的列称为foo_id.实际上,执行references :foo和仅执行integer :foo_id有什么区别吗?也许是在后台执行某些事情以在数据库级别强制执行该关系?

When I use references :foo in a migration, the column that's generated is called foo_id. Is there actually any difference between doing references :foo and just doing integer :foo_id? Maybe something going on under the hood to enforce the relationship at the database level?

推荐答案

对于您的特定情况,结果是相同的;你是对的.但是references允许使用:polymorphic => true选项,该选项将自动在表中将foo_type列创建为字符串.

The result is the same for your specific case; you are correct. But references allows for a :polymorphic => true option which will automatically create the foo_type column as a string in the table.

从语义上讲,如果尝试使迁移更好地反映数据库中表之间的关系,则references会更好.

Semantically, references is better if you are trying make your migrations better reflect the relations between tables in the database.

这篇关于Rails:'references:foo'和'integer:foo_id'之间有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆