Commit d270b857 authored by Andreas Brandl's avatar Andreas Brandl

Nested transaction for find_or_create_by! queries.

For background see
https://apidock.com/rails/v4.2.7/ActiveRecord/Relation/find_or_create_by.
parent e83e85ce
...@@ -23,14 +23,20 @@ class UserInteractedProject < ActiveRecord::Base ...@@ -23,14 +23,20 @@ class UserInteractedProject < ActiveRecord::Base
} }
cached_exists?(attributes) do cached_exists?(attributes) do
transaction(requires_new: true) do
begin begin
find_or_create_by!(attributes) where(attributes).select(1).first || create!(attributes)
true # not caching the whole record here for now true # not caching the whole record here for now
rescue ActiveRecord::RecordNotUnique rescue ActiveRecord::RecordNotUnique
# Note, above queries are not atomic and prone
# to race conditions (similar like #find_or_create!).
# We retry and make sure the outer transaction (if any)
# is not aborted because of this.
retry retry
end end
end end
end end
end
# Check if we can safely call .track (table exists) # Check if we can safely call .track (table exists)
def available? def available?
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment