跟踪类实例及其关联的变化 - 想法?

发布于 2024-09-09 21:06:20 字数 710 浏览 0 评论 0原文

我有一个类问题,其中有很多关联的模型。在我的应用程序的一页上,我列出了所有当前问题的摘要,以及相关记录中的各种信息。最终,这是一个值的哈希值,然后我将其打印到 csv 样式的行中(从这里起我将其称为“行哈希值”),

我现在需要仅显示更改给定时期内的问题(或其相关数据)。我目前正在考虑做到这一点的最佳方法。以下是我到目前为止的一些想法,欢迎任何反馈、想法、建议等。

1) 方法 1 -acts_as_audited 这是我的第一个想法,因为我之前在其他应用程序中使用过它。但 aaa 的问题是它只跟踪记录数据的更改(即它不关心关联是否更改)。因此,我也可以审核所有相关记录,但随后尝试通过将不同的审核记录捆绑在一起来拼凑出发生的变化,这听起来像是一场噩梦。

2)将旧的和新的散列保存到序列化字段中:即 - 当有人进入问题/编辑页面时,我计算当前行哈希并将其保存在问题表中的序列化字段“old_data”中。然后,在他们保存问题后,我计算新的当前行哈希并将其保存到问题表中的序列化字段“new_data”中。另外,我比较两个序列化哈希并将差异保存到另一个序列化哈希字段“更改”中。现在,为了做我的报告,我只查找最近 x 天内更新的问题并输出其更改数据。

3) 进行视图 - 我创建一个与我想要输出的数据相对应的视图(即合并我提取到报告中的所有数据)。然后我以某种方式跟踪视图的变化。我不确定我到底会怎么做。

我现在倾向于选项2。

还有其他想法/评论吗?感谢您的任何建议 - 最多。

I have a class Question which has a lot of assocated models. On one page on my app i list a summary of all the current questions, with various info from associated records. Ultimately this is a hash of values that i then just print out into a csv-style row (i'll call this the 'row hash' from hereon)

I now have a requirement to only show the changes to questions (or their associated data) over a given period. I'm currently deliberating the best way to do this. Here's some ideas i've had so far, i'd welcome any feedback, thoughts, suggestions etc.

1) Approach 1 - acts_as_audited
This was my first thought as i've used this before in other apps. The porblem with aaa though is that it only tracks changes to the record's data (ie it doesn't care if the associations change). So, i could audit all of the associated records as well but then trying to piece together what had changed by tying different audit records together sounds like a nightmare.

2) Save the old and new hash out into serialized fields: ie
- when someone goes to the question/edit page, i calculate the current row hash and save it in a serialized field "old_data" in the question table. Then after they save the question i calculate the new current row hash and save it into a serialized field "new_data" in the question table. Also, i compare the two serialized hashes and save the differences into another serialized hash field 'changes'. Now to do my report i just look for questions updated in the last x days and output their changes data.

3) make a view
- i make a view which corresponds to the data that i want to output (ie that amalgamates all the data that i pull into my report). Then i track changes to the view - somehow. I'm not sure how exactly i would do that.

I'm leaning towards option 2 right now.

Any other thoughts/comments? grateful for any suggestions - max.

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

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

发布评论

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

评论(3

内心激荡 2024-09-16 21:06:20

所以,就像你说的,你只想显示时间 x 和时间 y 之间记录的更改,对吧?使用acts_as_audited插件对我来说似乎很完美,因为你最终会得到一个更改表,对吗?因此,从问题到所有这些相关表建立一个 has_many_through 关联,然后搜索相关更改,其中创建日期在时间 X 之后。这将返回更改列表。从那里,如果需要的话,您可以将此列表连接回父对象,或者无论如何 - 但最终它似乎是一个更合理的搜索对象。您不是在寻找相关对象的列表,毕竟,您是在寻找更改列表,因此拥有更改表似乎是实现这一目标的合理方法?

So, like you said, you only want to show changes to the records between time x and time y, right? This would seem perfect to me using the acts_as_audited plugin because you end up with a table of changes, right? So make a has_many_through association from Question to all these related tables, then search it for related changes, where date created is after time X. This would return a list of changes. From there, you could connect this list back to the parent object if you need to, or whatever - but it in the end seems like a more reasonable thing to search. You're not looking for a list of related objects, after all, you're looking for a list of changes, so having a table of changes seems a reasonable way to accomplish that?

西瑶 2024-09-16 21:06:20

嘿,我遇到了类似的问题,请检查这个。如果可以的话,请使用 Mongoid 或 Mongomapper,嵌入式版本化文档非常有用。

Hey I had a similar problem, check this out. If you can, go with Mongoid or Mongomapper, embedded versioned documents are sweet.

小女人ら 2024-09-16 21:06:20

谢谢你们。我最终推出了自己的解决方案,因为我真正需要做的是捕获对象上调用的各种方法的结果的变化,其中一些方法涉及关联的对象。我对关联对象不太感兴趣,因为(例如)由于查看几个不同的关联对象而生成的文本字符串。我已经有了完成所有这些工作的方法,所以我实际上只需要跟踪调用这些方法的结果的变化。

我看到的插件都不能真正简单有效地做到这一点,因此我最终制作了一个名为 states 的表,该表仅保存包含所有这些方法调用结果的序列化哈希。当记录被更改和保存或任何相关关联对象被更改和保存时,此信息将被保存。然后我有一些方法来返回不同保存状态记录之间的差异。它很适合我的需要。不管怎样,非常感谢你的建议。

Thanks guys. I ended up rolling my own solution because what i really needed to do was to capture changes in the results of various methods called on the object, some of which involved associated objects. I wasn't so much interested in the associated objects as (for example) a text string generated as a result of looking at a few different associated objects. I had methods to do all of this already so i really just needed to track changes in the results of calling these methods.

None of the plugins i saw could really do that simply and effectively, so i ended making a table called states which just holds a serialized hash with results of all of these method calls. This gets saved when the record is altered and saved or when any of the relevant associated objects get altered and saved. Then i have some methods to return the differences between different saved state records. It works well for my needs. Thanks very much for your advice anyway.

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