我们总是使用外键的引用类型吗?

时间:2013-03-09 04:51:32

标签: ruby-on-rails

正如标题所说,我们总是使用外键的类型引用吗?

1 个答案:

答案 0 :(得分:1)

不,你不必。

如Rails指南中所述: http://guides.rubyonrails.org/migrations.html#special-helpers

  

另一个帮助程序称为引用(也可用作belongs_to)。在   它最简单的形式只是增加了一些可读性。

另一种选择,在迁移中你可以声明:

t.integer :account_id # where :account_id will hold the id being referenced to for a belongs_to

所以举个例子。导轨示例如下:

create_table :products do |t|
  t.references :category
end

但你也可以这样做:

create_table :products do |t|
  t.integer :category_id
end
相关问题