简体   繁体   English

如何在创建新用户时为paper_trail设置whodunnit?

[英]How to set whodunnit for paper_trail when creating a new user?

The paper_trail gem tracks versions, and does a good job. paper_trail gem跟踪版本,并且做得很好。 However, there is one edge case I've been noticing. 但是,我注意到有一个边缘情况。 For most objects, the application controller sets whodunnit if you are logged in, and then all objects that are created during that session have a version that records "whodunnit" according to who is logged in. 对于大多数对象,应用程序控制器会在您登录时设置whodunnit,然后在该会话期间创建的所有对象都具有根据登录者记录“whodunnit”的版本。

The interesting case is where nobody is logged in because a new user is signing up. 有趣的情况是没有人登录,因为新用户正在注册。 The user is created with a "nil" whodunnit, which is wrong because actually the user was created by themselves. 用户使用“nil”whodunnit创建,这是错误的,因为实际上用户是由他们自己创建的。

Of course, whodunnit can't know the id of the user before the user record is saved. 当然,在保存用户记录之前,whodunnit无法知道用户的id。 I know this. 我知道这个。

However, this creates a conflict later, as various batch jobs also modify user records, and not being in a web session, also create versions with nil whodunnit records. 但是,这会在以后产生冲突,因为各种批处理作业也会修改用户记录,而不是在Web会话中,也会创建具有nil whodunnit记录的版本。

Now I can't tell who created the user - some batch import process, or the user. 现在我无法分辨谁创建了用户 - 某些批量导入过程或用户。

I'm pondering various solutions, like perhaps rummaging around the Papertrail::Versions table for that object and fixing whodunnit, but that seems pretty unclean. 我正在考虑各种解决方案,比如可能围绕Papertrail :: Versions表搜索该对象并修复whodunnit,但这看起来非常不洁净。

Any advice? 有什么建议?

You can force whodunnit in the create action on your controller. 您可以在控制器上的create动作中强制执行whodunnit。

before_filter :only => [:create] do
  PaperTrail.whodunnit = "Public User"
end

If you insist on having the user id in the version table, you can do this: 如果您坚持在版本表中拥有用户标识,则可以执行以下操作:

ActiveRecord::Base.transaction do
  @user.save!
  @user.versions.last.update_attributes!(:whodunnit => @user.id)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM