簡體   English   中英

Rails的歸屬關系vs父標識

[英]Rails belongs_to vs parent_id

常規活動記錄問題:
-相對於parent_id列的belongs_to關聯的優缺點?
-關於2的約定?
-belongs_to關聯是否執行:null => false

class CreateIssues < ActiveRecord::Migration

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

    t.timestamps
  end
end

VS

class CreateIssues < ActiveRecord::Migration

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

    t.timestamps
  end
end

非常感謝!

nobody_to()只是references()的別名,不會強制執行(:null => false)條件。 查看源代碼

我發現使用references()更常見,但是再次,belongs_to()是有效的別名。

手動寫出引用是不常見的,因為它是ActiveRecord簡化這些關聯的一部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM