views:

21

answers:

1

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
+2  A: 

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
jigfox
With Vestal Versions, if I end up changing the versioned model's columns, does that mean all existing versions would become unusable?
WIlliam Jones
Sorry, but I don't understand your question here, but I hope my answer will still help. Vestal Versions keeps versions of your models, every time you update your model Vestal Version saves the old version, so you can access it at any time. So they won't get unusable
jigfox
I was just a little concerned about the fragility of the versions. If I were to add or a remove a column from the table for the base, I was wondering what would happen to the saved versions.
WIlliam Jones
okay, now I understand your concerns, sorry, but I can't help you here. I've never changed a versioned model like this.
jigfox