开发者

Insert delayed in rails

开发者 https://www.devze.com 2023-01-15 11:29 出处:网络
how to do? there开发者_高级运维 is an active record option?You can do it using rails monkey patch:

how to do? there开发者_高级运维 is an active record option?


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'


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号