<commit_before>class Batch < ActiveRecord::Base
  has_many :tickets
  belongs_to :ticket_kind

  named_scope :ordered, :order => 'created_at desc'

  validates_presence_of :ticket_kind_id
  validates_presence_of :emails

  before_validation :create_tickets
  after_validation :validate_tickets

  protected

  # Create the tickets associated with this batch.
  def create_tickets
    for email in self.emails.split(/\s+/).map(&:strip)
      self.tickets << Ticket.new(:email => email, :ticket_kind => self.ticket_kind, :batch => self)
      Ticket.find_all_by_email(email).each do |prev_ticket|
        # TODO how to pass this message to controller/view
        logger.warn "Warning: This email already has a #{prev_ticket.ticket_kind.title.upcase} ticket code, emailed status = #{prev_ticket.status.to_s.upcase}"
      end
    end
  end

  # Validate the associated tickets and add validation errors if needed.
  def validate_tickets
    if self.errors[:tickets]
      self.errors.clear # TODO Do we lose other non-ticket errors this way?
      for ticket in self.tickets
        next if ticket.valid?
        self.errors.add_to_base("Ticket with address '#{ticket.email}': " + ticket.errors.full_messages.join(', '))
      end
    end
  end
end
<commit_msg>Igal answered my question on this - we remove all errors but then add them back in for each ticket.<commit_after>class Batch < ActiveRecord::Base
  has_many :tickets
  belongs_to :ticket_kind

  named_scope :ordered, :order => 'created_at desc'

  validates_presence_of :ticket_kind_id
  validates_presence_of :emails

  before_validation :create_tickets
  after_validation :validate_tickets

  protected

  # Create the tickets associated with this batch.
  def create_tickets
    for email in self.emails.split(/\s+/).map(&:strip)
      self.tickets << Ticket.new(:email => email, :ticket_kind => self.ticket_kind, :batch => self)
      Ticket.find_all_by_email(email).each do |prev_ticket|
        # TODO how to pass this message to controller/view
        logger.warn "Warning: This email already has a #{prev_ticket.ticket_kind.title.upcase} ticket code, emailed status = #{prev_ticket.status.to_s.upcase}"
      end
    end
  end

  # Validate the associated tickets and add validation errors if needed.
  def validate_tickets
    if self.errors[:tickets]
      self.errors.clear
      for ticket in self.tickets
        next if ticket.valid?
        self.errors.add_to_base("Ticket with address '#{ticket.email}': " + ticket.errors.full_messages.join(', '))
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.0889498432601881, 'fraction_numerical': 0.0, 'mean_word_length': 3.583482944344704, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '2053187', 'n_tokens_mistral': 803, 'n_tokens_neox': 771, 'n_words': 225}