如何使用 ActiveRecord 处理 Wiki 的修订?
我有一篇模型文章,有很多修订。修订版有各种列,存储有关文章的所有信息。该文章还属于当前修订版,它是当前所选修订版的主键。每个修订版在创建后就永远不会更改。
当用户去编辑文章时,我想显示一个表单,其中显示修订版中的所有字段,并预先填充了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有内置方法用于检测 Active Record 中的更改。
API 有关于跟踪更改的良好文档: http://ar.rubyonrails.org/类/ActiveRecord/Dirty.html。
就您而言,只需检测文章中哪些字段已更改,然后从中创建新的修订即可。
大致如下:
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: