Commit 048edfe0 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Avoid subtransaction in UserInteractedProject

Use `insert_all` which ignores unique constraint violations instead of
doing this manually with a subtransaction

Changelog: performance
parent 676c2ef8
...@@ -24,16 +24,8 @@ class UserInteractedProject < ApplicationRecord ...@@ -24,16 +24,8 @@ class UserInteractedProject < ApplicationRecord
} }
cached_exists?(**attributes) do cached_exists?(**attributes) do
transaction(requires_new: true) do where(attributes).exists? || UserInteractedProject.insert_all([attributes], unique_by: %w(project_id user_id))
where(attributes).select(1).first || create!(attributes) true
true # not caching the whole record here for now
rescue ActiveRecord::RecordNotUnique
# Note, above queries are not atomic and prone
# to race conditions (similar like #find_or_create!).
# In the case where we hit this, the record we want
# already exists - shortcut and return.
true
end
end end
end end
......
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