Sha256: 90d74825e422f2bde19c7fc6998e9d9f6e579ab8ce12fda0397788afbe7a1b1f
Contents?: true
Size: 1.18 KB
Versions: 6
Compression:
Stored size: 1.18 KB
Contents
# frozen_string_literal: true module Thredded class UserTopicFollow < ActiveRecord::Base enum reason: %i[manual posted mentioned auto] belongs_to :user, inverse_of: :thredded_topic_follows, class_name: Thredded.user_class_name belongs_to :topic, inverse_of: :user_follows validates :user_id, presence: true validates :topic_id, presence: true # shim to behave like postable-related (though actually only ever related to topic) alias_attribute :postable_id, :topic_id alias_attribute :postable, :topic # 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. # # @return [Thredded::UserTopicFollow] def self.create_unless_exists(user_id, topic_id, reason = :manual) transaction(requires_new: true) do create_with(reason: reason).find_or_create_by(user_id: user_id, topic_id: topic_id) end rescue ActiveRecord::RecordNotUnique # The record has been created from another connection, retry to find it. retry end end end
Version data entries
6 entries across 6 versions & 1 rubygems