如何使用 ActiveRecord 处理 Wiki 的修订?

发布于 2024-08-19 02:08:19 字数 313 浏览 4 评论 0原文

我有一篇模型文章,有很多修订。修订版有各种列,存储有关文章的所有信息。该文章还属于当前修订版,它是当前所选修订版的主键。每个修订版在创建后就永远不会更改。

当用户去编辑文章时,我想显示一个表单,其中显示修订版中的所有字段,并预先填充了 current_revision 中的信息。这很简单,但是当用户保存时,我想将每个字段与 current_revision 中的值进行比较。如果所有字段都相同,我不想执行任何操作并丢弃表单帖子。但是,如果任何字段不同,我想创建一个新的修订版,而不是写入先前的修订版。

除了手动对修订版的每个字段进行硬编码测试之外,如何检测是否有任何字段已更改?

I have a model Article, that has_many Revisions. Revisions have a variety of columns storing all the information about the Article. The Article also belongs_to a current_revision, which is the primary key of the Revision that is currently selected. Each Revision is never changed after being created.

When a user goes to edit an Article, I want to display a form that shows all the fields that are in revisions, pre-populated with that information from the current_revision. That's simple enough, but when the user goes to save, I want to compare each field to the value in the current_revision. If all of the fields are the same, I want to do nothing and discard the form post. However, if any of the fields are different, I want to create a new Revision instead of writing to the previous Revision.

How can I detect whether any field has changed except by manually hard-coding a test for each field of the Revision?

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

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

发布评论

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

评论(1

七颜 2024-08-26 02:08:19

有内置方法用于检测 Active Record 中的更改。

API 有关于跟踪更改的良好文档: http://ar.rubyonrails.org/类/ActiveRecord/Dirty.html

就您而言,只需检测文章中哪些字段已更改,然后从中创建新的修订即可。

大致如下:

article= Article.find(id)
article.attributes = params[:article]
if article.changed?  
  //new revision
end

There are built in methods for detecting changes in Active Record.

The API has good documentation on the tracking changes: http://ar.rubyonrails.org/classes/ActiveRecord/Dirty.html.

In your case, it should simply be a matter of detecting which fields have changed in the Article, and then creating a new Revision from that.

Something along the lines of:

article= Article.find(id)
article.attributes = params[:article]
if article.changed?  
  //new revision
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文