Well, I've narrowed this down to an after_create problem. I'm
versioning the documents and each version depends on the most recent
version created. So there is an after_create method that looks like
this:
##############################################################
class Document < ActiveRecord::Base
belongs_to :projects
has_one :page
acts_as_tree
def after_create
update_version
end
def update_version
self.update_attribute("version", self.parent.next_version)
end
def next_version
# This used to be self.children.sort etc.. but I wanted to use
the :lock functionality
sorted_subversions = Document.find(:all, :conditions =>
["parent_id = ?", self.id], :lock => "FOR UPDATE")
# If it has no parent, then it is the root and doesn't need a
version.
if self.parent_id = nil
next_version = nil
# If there are children
elsif !sorted_subversions.empty?
sorted_subversions = sorted_subversions.sort {|x,y|
x.version.to_i <=> y.version.to_i }
latest_sub_version = sorted_subversions[-1] # The last in the
array sorted by version
if sorted_subversions.length == 1 &&
latest_sub_version.version.nil?
next_version = '1'
else
next_version = latest_sub_version.version.next
end
else
# If it is not a root, and doesn't have any children
if self.version
next_version = self.version + '.1'
else
next_version = '1'
end
end
return next_version
end
end
##############################################################
I removed this after_update method and everything is fine. The
problem is that I'm not sure how to do this without causing the
errors. The record needs to be updated with it's version after it is
created, depending on what versions already exist in the table. I
tried doing it before, but I was getting duplicate versions if two
documents were saved at the same time. With the :lock option, it
prevents duplicates, but causes the problem described at the beginning
of this thread.
Any help is GREATLY appreciated and thanks for all the help so far.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---