<commit_before># Every time a team captain invites a user to their team, a new user invite is created

class UserInvite < ActiveRecord::Base
  belongs_to :team
  belongs_to :user

  enum status: [ :Pending, :Accepted, :Rejected ]

  after_create :send_email, :link_to_user

  before_validation { self.email = self.email.downcase }

  validates_presence_of :email, :team

  validates :email, :email => true

  validates :status, inclusion: { in: statuses.keys }

  # Get only invites in the pending status.
  scope :pending, -> {where(status: 'Pending')}

  private

  def send_email
    UserMailer.invite_user(self).deliver_now
  end

  def link_to_user
    User.where(email: self.email).each do |user|
      user.user_invites << self
      user.save
    end
  end
end
<commit_msg>Rewrote link_to_user to not take a convoluted route, just directly update the user attribute.<commit_after># Every time a team captain invites a user to their team, a new user invite is created

class UserInvite < ActiveRecord::Base
  belongs_to :team
  belongs_to :user

  enum status: [ :Pending, :Accepted, :Rejected ]

  after_create :send_email, :link_to_user

  before_validation { self.email = self.email.downcase }

  validates_presence_of :email, :team

  validates :email, :email => true

  validates :status, inclusion: { in: statuses.keys }

  # Get only invites in the pending status.
  scope :pending, -> {where(status: 'Pending')}

  private

  def send_email
    UserMailer.invite_user(self).deliver_now
  end

  def link_to_user
    update_attribute(:user, User.find_by_email(self.email))
  end
end

{'fraction_non_alphanumeric': 0.08766437069505323, 'fraction_numerical': 0.0, 'mean_word_length': 3.9781931464174454, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4520675', 'n_tokens_mistral': 532, 'n_tokens_neox': 512, 'n_words': 159}