如何在 Rails 应用程序中构建审计跟踪
我有一个小型 Rails 应用程序。我想在其中添加审计跟踪。基本上是在添加新用户时。它将在 AuditTrailUsers
表中插入一行,其中包含创建并登录用户的 user_id 的新 user_id。
我正在考虑为此使用 Rails 回调 before_save
。但是,我不确定这是否有效。
想象一下,我有 model/Users.rb
和 model/AuditTrailUser.rb
class User < ActiveRecord::Base
validates_presence_of :userid, :password
before_save :insert_audit
def self.authenticate(userid, password)
user = self.find_by_userid_and_password(userid, password)
user
end
##implement insert_audit
end
我如何实现 insert_audit
以便它接受用户 ID(登录用户),以便在调用 AuditTrailUser.create(...)
时将其传递给 AuditTrailUser。
如果我传递登录用户的用户ID...那么我必须在每个地方显式调用before_save
...
我是rails新手。
I have a small rails app. I want to put audit trail in it. Basically when a new user is added. it will insert a row in AuditTrailUsers
table with the new user_id created and logged in users' user_id.
I am thinking about using rails callback before_save
for this. However, I am not sure if that will work.
Imagine I have model/Users.rb
and model/AuditTrailUser.rb
class User < ActiveRecord::Base
validates_presence_of :userid, :password
before_save :insert_audit
def self.authenticate(userid, password)
user = self.find_by_userid_and_password(userid, password)
user
end
##implement insert_audit
end
How can I implement insert_audit
so that it takes in a user id (of logged in user) so that it can pass it to AuditTrailUser when calling AuditTrailUser.create(...)
.
If I pass in logged in user's user id...then will I have to explicitly call before_save
every where...
I am new to rails.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 Audited gem 记录对 Rails 模型的所有更改。
Use the Audited gem to log all changes to your Rails models.