2013-02-10 58 views
0

在軌中如何生成唯一的鍵?非空的唯一鍵軌

我有說The backend will have a relational DB, with one table with the following schema: "user : string, password : string, count : int", with user enforced to be non-empty, a unique key.

一個規範這是否意味着如果我使用

rails generate scaffold UserModel user:string password:string count:int 

唯一鍵會爲我生成?它會默認有一個非空的要求嗎?

或者我需要

rails generate scaffold UserModel user:string password:string count:int unique_key int 
+0

您不需要顯式聲明唯一鍵,遷移將在運行時自動創建它。另外你爲什麼要調用你的用戶模型 - 「UserModel」?你可以稱它爲「用戶」。所以'rails g scaffold用戶用戶:string password:string' – Andrei 2013-02-10 18:34:56

回答

1

Rails會爲您生成密鑰,並會在內部處理它們。沒有必要明確包含它們。

所以,你可以簡單地寫類似:

rails generate scaffold User user:string password:string count:int 
1

Rails會自動與發電機是獨特不爲空創建id場。這應該符合你的要求。