简体   繁体   English

将更改记录到数据库

[英]Log changes to Database

I have around 15 more or less complex models. 我大约有15种复杂的模型。 Of those I would like to track the changes. 我想跟踪其中的变化。 What's the simplest way? 最简单的方法是什么? After-save/-update triggers? 后保存/更新触发器?

Performance is important but if there is a way with low complexity but decreasing performance, I'll do it. 性能很重要,但是如果有一种方法可以降低复杂性但降低性能,那么我会做的。

By the way: I do not want to use stored procedures. 顺便说一句:我不想使用存储过程。 (Though I might, if there is a really simple way to do so...) (尽管我可以,如果有一种非常简单的方法可以做到这一点...)

Thanks, Philip 谢谢菲利普

There is verstal_versions , which seems to do what you want to do. verstal_versions ,它似乎可以完成您想做的事情。

It adds versionning over all the models you specify. 它会在您指定的所有模型上添加版本控制。 See, for example : 参见,例如:

class User < ActiveRecord::Base
  versioned

  validates_presence_of :name
end

Then you can use your model's versions like this : 然后您可以使用模型的版本,如下所示:

>> u = User.create(:name => "Steve Richert")
=> #<User first_name: "Steve", last_name: "Richert">
>> u.version
=> 1
>> u.update_attribute(:name, "Stephen Richert")
=> true
>> u.name
=> "Stephen Richert"
>> u.version
=> 2
>> u.revert_to(10.seconds.ago)
=> 1
>> u.name
=> "Steve Richert"

You might also want to look at Dirty objects provided by ActiveRecord out of the box. 您可能还想直接查看ActiveRecord提供的Dirty对象 This gives you the ability to work with objects while they're in the process of being saved so that you can log the changes to a separate table. 这使您能够在对象处于保存过程中使用它们,以便您可以将更改记录到单独的表中。

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

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