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,18 +24,10 @@ class UserInteractedProject < ApplicationRecord
}
cached_exists?(**attributes) do
transaction(requires_new: true) do
where(attributes).select(1).first || create!(attributes)
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.
where(attributes).exists? || UserInteractedProject.insert_all([attributes], unique_by: %w(project_id user_id))
true
end
end
end
private
......
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