开发者

Insert delayed in rails

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜