module Tsuga::Adapter::ActiveRecord::Base::DatasetMethods

Public Instance Methods

collect_ids() click to toggle source
# File lib/tsuga/adapter/active_record/base.rb, line 38
def collect_ids
  pluck(:id)
end
mass_create(new_records) click to toggle source
# File lib/tsuga/adapter/active_record/base.rb, line 21
def mass_create(new_records)
  return if new_records.empty?

  # Old SQLite versions (like on Travis) do not support bulk inserts
  if connection.class.name !~ /sqlite/i || connection.send(:sqlite_version) >= '3.7.11'
    _bulk_insert(new_records)
  else
    new_records.each(&:save!)
  end
end
mass_update(records) click to toggle source
# File lib/tsuga/adapter/active_record/base.rb, line 32
def mass_update(records)
  transaction do
    records.each(&:save!)
  end
end

Private Instance Methods

_bulk_insert(records) click to toggle source
# File lib/tsuga/adapter/active_record/base.rb, line 44
      def _bulk_insert(records)
        attributes = records.map(&:attributes)
        keys = attributes.first.keys - ['id']
        column_names = keys.map { |k| connection.quote_column_name(k) }.join(', ')
        sql = <<-SQL
          INSERT INTO #{quoted_table_name} (#{column_names}) VALUES
        SQL
        value_template = (["?"] * keys.length).join(', ')
        value_strings = attributes.map do |attrs|
          values = keys.map { |k| attrs[k] }
          sanitize_sql_array([value_template, *values])
        end
        full_sql = sql + value_strings.map { |str| "(#{str})"}.join(', ')
        connection.insert_sql(full_sql)
      end