# Multiple models in one form concurrency problem

**URL:** <https://www.ruby-forum.com/t/multiple-models-in-one-form-concurrency-problem/171324>\
**Category:** Rails\
**Created:** [June 30, 2009, 6:52pm UTC](https://www.ruby-forum.com/t/multiple-models-in-one-form-concurrency-problem/171324 "2009-06-30T18:52:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [June 30, 2009, 6:52pm UTC](https://www.ruby-forum.com/t/multiple-models-in-one-form-concurrency-problem/171324/1 "2009-06-30T18:52:19Z")

</div>

I’m using the Advanced Rails recipe ‘Handle multiple models in one  
form’ and everything works as expected until I start concurrency  
testing.

For example I have a Task model and a Server model where a task can  
have multiple servers. Now if user1 and user2 edit task 100 at the  
same time, user1 adds a new server to the task and saves, everything  
is as it should be, however, user2 now simply changes something in the  
task and saves and now the task has lost the extra server user1 added.

I’m currently using ROR 2.3.2 and I’ve got optimistic locking on both  
models.

my code for saving eixisting servers is

def existing\_server\_attributes=(server\_attributes)  
servers.reject(&:new\_record?).each do |server|  
attributes = server\_attributes[server.id.to\_s]  
if attributes  
server.attributes = attributes  
else  
servers.delete(server)  
end  
end  
end

So the code works exactly as its supposed to. It gets all the servers  
for a task, compares that to the passed in set of existing servers and  
deletes any servers that aren’t in the list of existing servers.  
Except that its now deleting the newly created server that user1  
added. Is there a neat solution to this as for the moment I can’t  
think of one, or is it a case of going back to the original Bates  
recipe and marking the server as deleted with a virtual attribute and  
only deleting those servers? Should the servers.reject stop this  
problem but it isn’t working for some reason? Any help is much  
appreciated. Thanks.
