简体   繁体   English

Rails的归属关系vs父标识

[英]Rails belongs_to vs parent_id

Generic active record questions: 常规活动记录问题:
- Advantages/disadvantages of belongs_to association vs a parent_id column? -相对于parent_id列的belongs_to关联的优缺点?
- Conventions regarding the 2? -关于2的约定?
- Does belongs_to association enforce :null => false ? -belongs_to关联是否执行:null => false

class CreateIssues < ActiveRecord::Migration

def change
  create_table :issues do |t|
    t.belongs_to :project

    t.timestamps
  end
end

VS VS

class CreateIssues < ActiveRecord::Migration

def change
  create_table :issues do |t|
    t.integer :project_id, :null => false

    t.timestamps
  end
end

Thanks alot! 非常感谢!

belongs_to() is just an alias to references(), which does not enforce the (:null => false) condition. nobody_to()只是references()的别名,不会强制执行(:null => false)条件。 Check out the source . 查看源代码

I find it's more common to use references(), but again, belongs_to() is a valid alias. 我发现使用references()更常见,但是再次,belongs_to()是有效的别名。

It's uncommon to see the reference written out manually, as its part of the way ActiveRecord simplifies these associations. 手动写出引用是不常见的,因为它是ActiveRecord简化这些关联的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM