收录日期:2019/06/26 10:41:31 时间:2010-06-22 20:36:02 标签:ruby-on-rails,wiki,nested-attributes

I'm building a wiki system for an application. The essence of the design is that there is an Article model, and each Article has_many Revisions. When it's time to display an Article, the most recent Revision is pulled up to get all relevant information.

This seems like a perfect case to use an accepts_nested_attributes_for so that editing the Article would accept the changes on behalf of the Revisions. However, I can't seem to figure out a way to keep all old Revisions, and have changes be made by creating a new Revision on every edit. Is there any way to make this work?

For those who prefer things less abstract:

class Article
  has_many :revisions
  has_one :current_revision, :class_name => "Revision", :order => "created_at DESC"
  #contains columns that are not tracked for revisions, such as the article's Url slug
end
class Revision
  belongs_to :article
  #contains basic columns like wiki article body
end

You don't need all this work, take a look at Vestal Versions gem. It brings all you need for versioned models. There is also a railscast for this.

But if you still want to work with nested forms there are two good railscasts:

  1. Nested Model Form Part 1
  2. Nested Model Form Part 2