【发布时间】:2015-04-22 23:05:46
【问题描述】:
由于在我们的 Rails 应用程序中删除用户的错误,我正试图将另一个用户记录强制使用旧记录 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 处找不到。就像它迷失在了边缘……
-
当你
replacer.save!时会发生什么?
标签: ruby-on-rails ruby-on-rails-4 clearance