<commit_before>class UserSubscription < ActiveRecord::Base
  validates :user_id, :subscription_id, presence: true
  belongs_to :user
  belongs_to :subscription
  after_create :send_premium_emails

  def self.update_or_create(user_id, subscription_id)
    user_sub = self.find_or_initialize_by(user_id: user_id)
    user_sub.subscription_id = subscription_id
    user_sub.save!
  end

  def send_premium_emails
    if Rails.env.production? || User.find(user_id).email.match('quill.org')
      if subscription.account_type.downcase != 'teacher trial' && subscription.school_subscriptions.empty?
        PremiumUserSubscriptionEmailWorker.perform_async(user_id)
      elsif subscription.account_type.downcase != 'teacher trial'
        PremiumSchoolSubscriptionEmailWorker.perform_async(user_id)
      end
    end
  end

end<commit_msg>Fix user subscription emails to only send on commit create instead of just create<commit_after>class UserSubscription < ActiveRecord::Base
  validates :user_id, :subscription_id, presence: true
  belongs_to :user
  belongs_to :subscription
  after_commit :send_premium_emails, on: :create

  def self.update_or_create(user_id, subscription_id)
    user_sub = self.find_or_initialize_by(user_id: user_id)
    user_sub.subscription_id = subscription_id
    user_sub.save!
  end

  def send_premium_emails
    if Rails.env.production? || User.find(user_id).email.match('quill.org')
      if subscription.account_type.downcase != 'teacher trial' && subscription.school_subscriptions.empty?
        PremiumUserSubscriptionEmailWorker.perform_async(user_id)
      elsif subscription.account_type.downcase != 'teacher trial'
        PremiumSchoolSubscriptionEmailWorker.perform_async(user_id)
      end
    end
  end

end
{'fraction_non_alphanumeric': 0.07613050944476245, 'fraction_numerical': 0.0, 'mean_word_length': 4.865771812080537, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3286371', 'n_tokens_mistral': 541, 'n_tokens_neox': 544, 'n_words': 102}