Class: Thredded::UserTopicFollow

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/thredded/user_topic_follow.rb

Class Method Summary collapse

Class Method Details

.create_unless_exists(user_id, topic_id, reason = :manual) ⇒ Thredded::UserTopicFollow

Creates a follow or finds the existing one.

This method is safe to call concurrently from different processes. Lookup and creation happen in a transaction. If an ActiveRecord::RecordNotUnique error is raised, the find is retried.


23
24
25
26
27
28
29
30
31
32
# File 'app/models/thredded/user_topic_follow.rb', line 23

def self.create_unless_exists(user_id, topic_id, reason = :manual)
  uncached do
    transaction(requires_new: true) do
      create_with(reason: reason).find_or_create_by(user_id: user_id, topic_id: topic_id)
    end
  end
rescue ActiveRecord::RecordNotUnique  # The record has been created from another connection, retry to find it.

  retry
end