简体   繁体   English

审计的audited_changes混乱

[英]Audited audited_changes confusion

Im using collectiveidea's audited solution for auditing in rails. 我正在使用collectionidea的审计解决方案来进行铁路审计。 So, there's a column (audited_changes) that is a TEXT definition in the database. 因此,在数据库中有一列(audited_changes)是TEXT定义。 When i retrieve an audit from the database, i get a plain string, and when i call that attribute in the view is non formated string. 当我从数据库中检索审计时,我得到一个纯字符串,而当我在视图中调用该属性时,它是未格式化的字符串。 In the rdocs it says that theres a serialized hash of all changes. rdocs中,它说存在所有更改的序列化哈希。 How can i get this hash? 我如何获得此哈希? Also, in the same docs it says that there's access to old_attributes and new_attributes, how is this? 另外,在同一文档中,它说可以访问old_attributes和new_attributes,这是怎么回事?

In my view: 在我看来:

<%= @audit.action %> # => update
<%= @audit.audited_changes %> # => --- name: - oldname - newname code: - oldcode - newcode

Or any chance on formatting this? 还是有格式化的机会?

I think there might currently be a bug in audited. 我认为目前可能存在经过审核的错误。 Are you using 3.0.0rc1? 您正在使用3.0.0rc1吗? That is what I am using and I had something similar happen. 那就是我正在使用的,并且发生了类似的事情。 First off, it didn't seem to recognize "Audit" as an ActiveRecord object, so I created an empty model in app/models/audit.rb. 首先,它似乎没有将“ Audit”识别为ActiveRecord对象,因此我在app / models / audit.rb中创建了一个空模型。 Once I did that I was seeing the behaviour you are seeing. 一旦这样做,我就会看到您所看到的行为。 To fix it, I removed the app/models/audit.rb and added an config/initializers/audited.rb with this in it: 要修复它,我删除了app / models / audit.rb并添加了一个config / initializers / audited.rb:

include Audited::Adapters::ActiveRecord

This is an old question but I have an alternative answer that seems to be working well for me using Rails 4.2. 这是一个老问题,但是我有一个替代答案,对于使用Rails 4.2的我来说似乎效果很好。 Instead of using the initializer in the above answer I suggest keeping the model and adding "serialize :audited_changes" to the top. 建议不要保留上面的答案中的初始化程序,而是建议保留模型并在顶部添加“ serialize:audited_changes”。

 class Audit < ActiveRecord::Base
   belongs_to :user
   serialize :audited_changes
 end

You could use the built-in Audited::Audit model to query its data. 您可以使用内置的Audited :: Audit模型来查询其数据。

For example, 例如,

audit = Audited::Audit.last
audit.audited_changes # => {"name"=>["Steve", "Ryan"]}

"Steve" is the old value, "Ryan" is the new value. “ Steve”是旧值,“ Ryan”是新值。 By default, the hash is stored in yaml format in the database. 默认情况下,哈希以yaml格式存储在数据库中。

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

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