Validations about uniqueness of things are prone to race conditions. If you need a cast iron guarantee that can only come from the database itself (ie unique index etc...)
Sent from my iPhone On 31 Dec 2008, at 21:04, [email protected] wrote: > > I'm running into a strange problem with transactions. I am seeing > rare duplicate entries in the DB that should be prevented by my > validations. Here is some sample code: > > class Review < ActiveRecord::Base > has_one :member > has_one :business > attr_accessor :sleep_for > > def validate > validate_uniqueness_combo_of_member_and_business > sleep(sleep_for) if self.sleep_for > end > end > > and here is that code executed: > > #console 1 >> r = Review.new(:member_id => 1, :business_id => 4) >> r.sleep_for = 5 >> r.save! > => true > > #console 2 (during the 5 sec sleep) >> r = Review.new(:member_id => 1, :business_id => 4) >> r.save! > => true > > I would expect the first save to lock the table and the second to fail > after 5 seconds. Instead they both succeed and I end up with bad data > in the db. It also fails if I wrap both save! calls in > Review.transaction blocks. > > I could not find docs on whether validate() is within the transaction, > but I would expect it to be. Any ideas on what is wrong here? > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

