Acts_as_versioned delete a specific version

June 26, 2009 at 11:56 pm Leave a comment

Environment: Rails 2.3.2, Windows Vista, SQLite3 development on a local box

By default acts_as_versioned has a method to delete up to :limit of versions called clear_old_versions. If you need to delete a specific version, the following does not work: @artist.versions.delete(id) because a String is passed instead of a Versioned class.

But you can specify your own method, which is a very easy modification of the clear_old_versions:

def clear_old_version(version)
     self.class.versioned_class.delete_all ["#{self.class.version_column} = ? and #{self.class.versioned_foreign_key} = ?", version, id]
end

And now you can call this method like this: @artist.clear_old_version(params[:version_id])

artist_controller.rb:

def deleteversion
   @artist = Artist.find(params[:artist_id])
   @artist.clear_old_version(params[:version_id])
   redirect_to :action => 'show', :id => @artist
end
Advertisements

Entry filed under: Uncategorized.

Post ruby or html, css, sql sourcecode in your wordpress.com blog Michael Jackson RIP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent posts

Starting to learn Rails?

Kindle

Get Kindle - the best e-book reader, that I personally use, and the only one that you can read on the beach - very useful: Kindle Wireless Reading Device (6" Display, Global Wireless, Latest Generation)

%d bloggers like this: