tapioco123
tapioco123

Reputation: 3545

Insert delayed in rails

how to do? there is an active record option?

Upvotes: 2

Views: 805

Answers (2)

brett-richardson
brett-richardson

Reputation: 106

MySQL also provides "INSERT DELAYED" which behaves a little differently from "LOW PRIORITY" I believe?

Upvotes: 1

Dmitry Polushkin
Dmitry Polushkin

Reputation: 3403

You can do it using rails monkey patch:

class ActiveRecord::Base
  def self.insert_low_priority(hash)
    keys = hash.keys.map { |v| "`#{v}`" }.join(',')
    values = hash.values.map { |v| sanitize(v) }.join(',')
    connection.insert_sql("INSERT LOW_PRIORITY INTO `#{table_name}` (#{keys}) VALUES(#{values})")
  end
end

Add it to the initializers, e.g. config/initializers/activerecord_insert_low_priority.rb

Usage is simple: ModelName.insert_low_priority :column1 => 'value'

Upvotes: 4

Related Questions