<commit_before>class Version < ActiveRecord::Base

  belongs_to :service
  has_many :headers
  validates :number, presence: true
  validates :total_records, presence: true
  has_many :updates, class_name: "VersionUpdate"

  before_create :set_initial_total_records

  def set_initial_total_records
    self.total_records = 0
  end

  def records
    Record.with(collection: self.collection)
  end

  def make_version_update(filename)
    self.updates << VersionUpdate.create(user_id: self.service.creator.id, filename: filename)
  end

  def insert_record(record)
    records.create!(record)
  end

  def collection
    service.slug.underscore.camelize + "V#{number}"
  end

  def set_total_records
    # TODO: replace with counter_column maybe?
    self.update(total_records: self.records.count)
  end

  # Faster but not yet working <----------------->
  # def create_records(file)
  #   count = total_records
  #   new_records = []
  #   CSV.foreach(file, {
  #       headers: true,
  #       header_converters: lambda { |h| h.downcase.gsub(' ', '_') unless h.nil? }
  #     }) do |record|
  #       record = record.to_hash
  #       record[:insertion_id] = count +=1
  #       new_records << record
  #   end
  #   records.collection.insert(new_records)
  #   set_total_records
  # end
  #                       <----------------->

  def create_records(file)
    count = total_records
    file.each do |row|
      row_hash = row.to_hash
      row_hash[:insertion_id] = count += 1
      insert_record(row_hash)
    end
    set_total_records
  end

end
<commit_msg>modified to use creator_id instead of creator.id<commit_after>class Version < ActiveRecord::Base

  belongs_to :service
  has_many :headers
  validates :number, presence: true
  validates :total_records, presence: true
  has_many :updates, class_name: "VersionUpdate"

  before_create :set_initial_total_records

  def set_initial_total_records
    self.total_records = 0
  end

  def records
    Record.with(collection: self.collection)
  end

  def make_version_update(filename)
    self.updates << VersionUpdate.create(user_id: self.service.creator_id, filename: filename)
  end

  def insert_record(record)
    records.create!(record)
  end

  def collection
    service.slug.underscore.camelize + "V#{number}"
  end

  def set_total_records
    # TODO: replace with counter_column maybe?
    self.update(total_records: self.records.count)
  end

  # Faster but not yet working <----------------->
  # def create_records(file)
  #   count = total_records
  #   new_records = []
  #   CSV.foreach(file, {
  #       headers: true,
  #       header_converters: lambda { |h| h.downcase.gsub(' ', '_') unless h.nil? }
  #     }) do |record|
  #       record = record.to_hash
  #       record[:insertion_id] = count +=1
  #       new_records << record
  #   end
  #   records.collection.insert(new_records)
  #   set_total_records
  # end
  #                       <----------------->

  def create_records(file)
    count = total_records
    file.each do |row|
      row_hash = row.to_hash
      row_hash[:insertion_id] = count += 1
      insert_record(row_hash)
    end
    set_total_records
  end

end

{'fraction_non_alphanumeric': 0.1141595711132135, 'fraction_numerical': 0.001892147587511826, 'mean_word_length': 2.771700356718193, 'pattern_counts': {'":': 0, '<': 17, '<?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': '2090977', 'n_tokens_mistral': 1039, 'n_tokens_neox': 1016, 'n_words': 220}