My Rails application includes multiple forms on one page which all relate to the same controller (and in fact the same association). I have one submit button which uses jQuery to submit all of the forms at once through AJAX. The problem is that only some of the updates are actually showing as taking effect in the database. They all use the same logic and the same method and one at a time, the data persists fine. But all sent at once, only about half of the data is actually kept in the database.
The Javascript looks like this:
$(document).on('click', '.submit', function(event){
$('form').submit();
});
So that submits all the forms via AJAX on one click. (Figure about 10 forms)
The Rails controller looks like this (simplified):
#prepare the JSON data
adapter = DataMapper.repository(:default).adapter
adapter.execute("UPDATE person SET facets=? WHERE id=?", JSON.generate(facets), id)
render :text => 'OK'
I think the problem may involve the fact that multiple processes are trying to access the method at the same time and updating the field with old data. I get the response 'ok' for every request, but only about half of the results take effect.
Any suggestions? Thanks!
EDIT:
Just as an update, in case anyone has this same problem. I fixed the issue in jQuery by setting async to false. This takes the browser a little longer to send and may hold up other javascript events until it's done, but none of the requests will interfere with one another.