database_cleaner-active_record
database_cleaner-active_record copied to clipboard
Problem with restarting a transaction when using transaction strategy
I have a method like this:
def make_foo(bar, params)
foo = nil
self.transaction do
foo = self.create!(name: bar) rescue nil
end
unless foo
self.transaction do
foo = Something.find_by(name: bar).update(params)
end
end
foo
end
This method relies on a unique index on name to raise PG:Error, in which it will update the object instead. This is all fine until I use transaction in database_cleaner and it complains about
I tried using truncation for the test, and it somewhat works, but I'm just wondering if this is something that can be supported (sounds like nested transaction?)? Essentially I'd like to get the transaction to restart within the test, if that's possible.
+1