rails中的Redis-Objects保存为String,而不是数组

时间:2012-06-17 16:26:59

标签: ruby-on-rails ruby arrays marshalling redis-objects

我使用redis-objects gem存储简单信息

class Purchase < ActiveRecord::Base
  include Redis::Objects
  hash_key :user_purchases, :marshal => true, :global => true  # "hash" is taken by Ruby

  def self.add_user_end(fb_id,item_id)
    if self.user_purchases[fb_id]
      a = Array.new
      a << candidate_id 
      self.user_purchases[fb_id] = a
    else
      new_a = self.user_purchases[fb_id]
      new_a << item_id
      self.user_purchases[fb_id] = new_a
    end
  end
end

我正在创建一个方法来收集user_purchases作为hasy_key,由用户fb id键入。当我使用Purchase.user_purchases [“2”] =&gt;时,我希望看到一组id。 [1,23,563,2]

我遇到了Redis :: Object的问题,我只能将其保存为字符串:为什么?

1.9.3-p125 :050 > Purchase.user_purchases["6"].class
 => String 
1.9.3-p125 :051 > Purchase.user_purchases["6"]
 => "\u0004\b[\u0006I\"\v543555\u0006:\u0006ET" 

答案:

我的初始化文件缺失需要'redis / list'

require 'redis'
require 'redis/objects'
require 'redis/hash_key'
require 'redis/list'

Redis.current = Redis.new(:host => '127.0.0.1', :port => 6379)

1 个答案:

答案 0 :(得分:1)

我的初始化文件缺失需要'redis / list'

require 'redis'
require 'redis/objects'
require 'redis/hash_key'
require 'redis/list'

Redis.current = Redis.new(:host => '127.0.0.1', :port => 6379)