Topic: keeping track of changes in sortable elements

Hey all,
I have a sortable element list that looks like this:

<ul id="yourranking" style=" list-style-type:none;      margin:0;      padding:0;">
<% @uranks.each do |u| %>
<li id="item_<%= u.id %>"> <%= u.rank %>   <%= u.item.title %> </li>
<% end %>
</ul>
<p id="list-info"></p>

<%= sortable_element 'yourranking',
      :update => 'list-info', 
      :complete => visual_effect(:highlight, 'yourranking'),
      :url => { :controller=>"lists",:action => "order", :listid=> 'yourranking'} %>
</fieldset>


this is the controller code:
def order
  params[params[:listid]].each_with_index { |id,idx| Urank.update(id, :rank => idx) }
end

I would like to keep track of the old ranking before the latest update like:

Urank.update(id,:rank=>idx,:oldrank=><old rank before update>)

any idea how to do that?

thanx in advance

Pat

Re: keeping track of changes in sortable elements

What you want is to add a before_filter action to your Urank model:

Urank < ActiveRecord::Base

  before_filter :copy_rank_to_old_rank

  def copy_rank_to_old_rank
    # we're going to find the old rank in the database
    # 'cause this object's rank has already been overwritten
    self.old_rank = find(id).rank
  end

end