如何在 Rails 应用程序中构建审计跟踪

发布于 2024-08-25 03:56:47 字数 787 浏览 3 评论 0原文

我有一个小型 Rails 应用程序。我想在其中添加审计跟踪。基本上是在添加新用户时。它将在 AuditTrailUsers 表中插入一行,其中包含创建并登录用户的 user_id 的新 user_id。

我正在考虑为此使用 Rails 回调 before_save 。但是,我不确定这是否有效。

想象一下,我有 model/Users.rbmodel/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花浅忆 2024-09-01 03:56:47

使用 Audited gem 记录对 Rails 模型的所有更改。

Use the Audited gem to log all changes to your Rails models.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文