Sha256: 8ad2dc26d548008cd0e0830c4384e8ab42a0b4ca9067fae2b620dd229af10583
Contents?: true
Size: 1.17 KB
Versions: 9
Compression:
Stored size: 1.17 KB
Contents
# frozen_string_literal: true module Thredded class UserTopicFollow < ActiveRecord::Base enum reason: [:manual, :posted, :mentioned] belongs_to :user, inverse_of: :thredded_topic_follows, class_name: Thredded.user_class 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
9 entries across 9 versions & 1 rubygems