Hi, I have a bunch of models that are all related to each other through various has_one, has_many, belongs_to relationships. I am trying to create a set of these related objects when parsing through an XML file, but would like to save the set as a whole through the use of transactions, to cut down on the number of SQL COMMITs that are called, as these are slowing down my app by a factor of 2 to 3 (compared to using transactions). However, as object.save is not being called until the transaction is processed, the foreign key constraints for the relationships are failing. At first I was getting the highest id value from the tables and using these to set the values. The problem now is that I am using BackgrounDRb to do the processing, and if several XML files come in in sequence, then two or more of the BackgrounDRb workers are pulling the same id value from the tables, as the transactions have not completed before the next worker is generated. Is there a way to get or set this auto-generated id value without saving the object, so that I can continue using transactions without causing this race condition? Thanks, Simon
on 2007-04-11 18:42
on 2007-04-11 21:26
Hi Simon, You can wrap a set of rails commands in a single transaction by doing... ActiveRecord::Base.transaction do <your code> end And it will only do one commit at the end. Hope this helps, Dave.