0
我創建Rails中在Rails中播種外鍵?
User has_one :profile
Profile belongs_to :user
據我瞭解,在正常情況下Rails會自動創建這兩種模式間的外鍵關係的用戶和其輪廓像這樣的關係。但是,當我用以下方式爲數據庫播種數據時,創建了許多用戶並創建了許多配置文件,那麼我如何確保Joe的配置文件與Joe的用戶帳戶鏈接。
User.create!(email: "[email protected]", password: "blahblah", encryptedpass: "3838")
Profile.create!(first_name: "Joe", last_name: "Frank", address: "43 Flint Road", phone: "604 345 678",)
User.create!(email: "[email protected]", password: "blahblahblah", encryptedpass: "4567")
Profile.create!(first_name: "Anna", last_name: "Jones", address: "43 Boogy Road", phone: "604 345 678",)
......想象成千上萬的用戶和配置文件以這種方式接種的....
我是不是要設置輪廓user_id
外鍵,播種它的目的,我只是組成一個外鍵ID?軌道可以爲我生成嗎?例如,如果我在Joe的個人資料上創建了一個外鍵作爲user_id: 1
我怎麼能確定他的用戶模型實例將是「1」換句話說,如果我手動設置他的user_id外鍵,我怎樣才能確保它會匹配用戶模型的ID嗎?