I want to accomplish the following. I have an object that can be edited. However, it can occur that two people might edit the object at the same time. Instead of locking the object down, I prefer to tell the user how their version differs from the last version that was committed.
Kinda like:
respond_to do |format| @document = Document.find_by_id(1) @document.attributes = params[:document] @changes = @document.diff(Document.find_by_id(1)) format.html { render :action => "edit" } end
The edit screen then renders the changes for the user to compare. The user can then apply some changes or ignore the changes and save.
This works fine for regular fields, but when the object has has_many assoctiations, then the following would occur.
respond_to do |format| @document = Document.find_by_id(1) @document.attributes = params[:document] # => this call would update the database for all has_many associations @changes = @document.diff(Document.find_by_id(1)) #=> retrieving the previous object state from the database is not possible since the associations have changed format.html { render :action => "edit" } end
I also tried to compare the object to the attributes kinda like this;
respond_to do |format| @document = Document.find_by_id(1) @changes = @document.diff(params[:document]) format.html { render :action => "edit" } end
But then the user's changes aren't stored on the object and all updates are lost.
Any help is appreciated.