确定模型内部正在调用哪个操作。红宝石 on Rails

发布于 2024-09-24 04:07:17 字数 291 浏览 0 评论 0原文

我如何知道是否从模型内部调用了创建或任何其他操作。基本上我正在做数据库日志记录,并希望跟踪是否正在执行创建或其他操作。

为了进行日志记录部分,我使用 ActiveRecord::Observer 的概念。但我无法确定用户是否正在创建或正在执行其他操作。所以请告诉我 Rails 为我们提供的一些方法来识别模型内部的动作。

提前致谢。

How do I come to know whether a create or any other action has been called from inside a model. Basically I am doing database logging and want to track whether a create or other actions are being performed.

For doing the logging part I am using the concept of ActiveRecord::Observer. But there I am not able to find out whether the user is creating or doing something else. So please tell me some way that rails provides us to identify the action inside the model.

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

独自唱情﹋歌 2024-10-01 04:07:18

您可以使用 before_save 在保存模型时触发事件。要确定它是保存还是创建,您可以使用此方法 new_record?它会告诉您它是否是一个新实例。您还可以知道哪些属性已更改

You can use a before_save to trigger an event when the model is saved. To determine if it is a save or a create you have this method new_record? which will tell you if it's a new instance or not. You can also know which attributes have changed

爱本泡沫多脆弱 2024-10-01 04:07:18

从体系结构的角度来看,不建议这样做,但这仅用于模型内部深处的低级别日志记录,并且不会造成任何损害。这也是线程安全的解决方案。

ApplicationController 或只是单个控制器:

around_filter :store_remote_ip_in_thread

def store_remote_ip_in_thread
  begin
    Thread.current[:remote_ip] = request.remote_ip
    yield
  ensure
    Thread.current.delete :remote_ip
  end
end

用于检索的可重用模块:

module RemoteIpAware
  def current_remote_ip
    Thread.current[:remote_ip] || '-'
  end
end

模型/邮件程序/lib 类中的某个位置:

 include RemoteIpAware
 ...
 #use current_remote_ip method anywhere

请记住,我是凭记忆写的。语法可能不正确;)
在这种情况下,您可以保存 params[:action] :控制器名称等,而不是远程 IP。

This is not recommended from architectural point of view but this is used only for low level logging deep inside Model and doesn't do any harm. Also this is thread-safe solution.

ApplicationController or just single controller:

around_filter :store_remote_ip_in_thread

def store_remote_ip_in_thread
  begin
    Thread.current[:remote_ip] = request.remote_ip
    yield
  ensure
    Thread.current.delete :remote_ip
  end
end

Reusable module for retrieval:

module RemoteIpAware
  def current_remote_ip
    Thread.current[:remote_ip] || '-'
  end
end

Somewhere in model/mailer/lib class:

 include RemoteIpAware
 ...
 #use current_remote_ip method anywhere

Keep in mind I wrote it from my memory.. syntax may not be ok ;)
In you case instead of remote IP you can save params[:action] :controller name etc..

丿*梦醉红颜 2024-10-01 04:07:17

这种跟踪应该在控制器上执行。
观察者仅具有模型感知能力,并且也应该仅具有模型感知能力。

考虑从控制台更新对象的情况。
观察者将被触发,但这里没有请求上下文。

Such this kind of tracking should be performed on the Controller.
Observers are only model-aware and should be model-aware only.

Consider the case where you are updating the object from the console.
The observer will be triggered, but you have no request context here.

如日中天 2024-10-01 04:07:17

应用程序中调用的任何操作都存储在 params[:action] 中,并且在模型中无法访问 params。
所以我认为您看不到从模型中调用了哪个操作。

谢谢,阿努巴

Which ever action is called in application is stored in params[:action] and params is not accessible in models.
So i don't think you can see which action is getting called from models.

Thanks,Anubhaw

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