Module: RedisAssist::Utils

Defined in:
lib/redis_assist/utils.rb

Class Method Summary collapse

Class Method Details

.rename_attribute!(options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/redis_assist/utils.rb', line 5

def rename_attribute!(options={})
  raise "model option must be provided" unless klass = options[:model]
  raise "from option must be provided"  unless from  = options[:from]
  raise "to option must be provided"    unless to    = options[:to]

  klass.find_in_batches do |batch|
    batch.each do |record|
      client        = record.redis
      attr_key      = record.key_for(:attributes)
      value         = client.hget(attr_key, from)

      if value 
        client.multi do |multi|
          multi.hset(attr_key, to, value)
          multi.hdel(attr_key, from)
        end
      end
    end
  end 
end