调整用户注册日期

大家好,

今天我在一次迁移后收到一个不寻常的请求,那次迁移中我们跳过了所有从未发布过内容的账户。

一位从未发布过内容但重视自己账户的用户希望找回他的账户。我告诉他可以直接用旧用户名重新注册,他也照做了。但现在他希望能将他的“注册时间”调整为2011年,即他注册旧账户的时间……

请问有什么方法可以实现这一点吗?

你说的“注册时间”是指账户摘要中的“加入”部分吗?

如果你拥有服务器或数据库的管理员权限,可以在 Users 表中编辑该用户的 created_at 参数。

你可以这样通过 Rails 控制台操作:

rails c
user = User.where(username: '<your-username-here>')
user.update(created_at: "2011-02-10 00:00:00.00000")

你也可以直接在 PostgreSQL 数据库中操作(虽然我不推荐这样做):

update public.users set created_at='2011-02-10 00:00:00.00000' where username = '<your-username-here>';

我在本地环境中测试过,效果正常:

修改前:

修改后:

如果你决定采用这种方法,请务必小心。建议在进行任何操作之前先备份数据库 :sweat_smile:

9 个赞

非常感谢,这正是我需要修改的参数。

3 个赞

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.