2
由於在我們的Rails應用程序中刪除用戶的錯誤,我試圖強制另一個用戶記錄到舊的記錄ID中。Rails:爲新記錄強制指定的id(或更改已有的id)
$ rails console
> User.find(2)
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=2
> replacer = User.find(5)
=> #<User id: 5, created_at: [omitted for brevity ...] >
replacer.id = 2
=> 2
replacer.save
=> true
> User.find(2)
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=2
> User.find(5)
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=5
> replacer
=> #<User id: 2, created_at: [omitted for brevity ...] >
> replacer.valid?
=> true
這是怎麼回事?
你不能從AR做它,在數據庫中正確執行它。 –
我之前用AR做過這樣的事情。不知道如何在數據庫中做到這一點奇怪的事情是記錄似乎已經保存但無法找到id:5 *或* id:2。這就像它是在迷失... – Meltemi
當你做'replacer .save!'? – 2015-04-23 04:59:55