Shagy Moe wrote:
>
> I think two queries at the same time would still return the same
> version.
Actually I was thinking to simplify, minimizing the time your spending
between the query and the return (you may still need the mutex as
suggested above under heavy load scenarios)
def next_version
next_version = nil
unless self.parent_id.nil?
last = Document.find(:first, :order => 'version desc', :conditions
-> ['parent_id = ?', self.id])
if last.nil?
next_version = (self.version.nil? ? '1' : self.version + '.1')
else
next_version = (last.version.nil? ? '1' : last.version.next)
end
end
return next_version
end
I *think* this is an equivalent translation of your code, one query, and
at most 2 tests before the answer is returned. I'm unsure whether the
order will works for your case, but try it out if you're interested.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---