Ruby on rails 从缓存访问对象属性时出现问题

Ruby on rails 从缓存访问对象属性时出现问题,ruby-on-rails,Ruby On Rails,我正在使用Rails.cache.write缓存多个用户对象,但当我从缓存中检索它并尝试调用任何用户对象的属性方法(id、名称、电子邮件等)时 You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.include? 我可以调用用户对象上的inspect,并查看所有属性。有什么

我正在使用
Rails.cache.write
缓存多个用户对象,但当我从缓存中检索它并尝试调用任何用户对象的属性方法(id、名称、电子邮件等)时

You have a nil object when you didn't expect it! 
You might have expected an instance of Array. 
The error occurred while evaluating nil.include? 
我可以调用用户对象上的inspect,并查看所有属性。有什么想法吗?谢谢

我使用的是标准缓存配置,没有修改。下面是一些代码:

我在一个实例方法中写入模型中的缓存,该实例方法在用户登录时被调用,如下所示:

cached_friends = self.friends
Rails.cache.write(self.id.to_s+"_friends", cached_friends)
然后在我的控制器中有一个前置过滤器:

def get_friends_from_cache
  if current_user
    friends = Rails.cache.read(current_user.id.to_s+"_friends")
    friends.each do |friend|
      if !friend.facebook_id.nil?
        #other stuff....
      end
    end
  end
end

我在
if上得到了错误!friend.facebook\u id.nil?
facebook\u id
是数据库中的一列。基本上没有可用于每个用户对象的用户实例方法。但是我知道对象在那里,因为我可以插入
raise“#{friends.first.inspect}”
,所有的东西都会像我期望的那样吐出来。

这在开发模式下发生(即设置config.cache_classes=false)。根本原因是:

  • ActiveRecord从类对象中剥离属性访问器方法
  • MemoryStore(即默认的缓存存储)不支持军事/非人工对象

  • 一种解决方法是使用MemCacheStore,另一种方法是设置config.cache_classes=true。另请参见

    这在开发模式下发生(即设置config.cache_classes=false)。根本原因是:

  • ActiveRecord从类对象中剥离属性访问器方法
  • MemoryStore(即默认的缓存存储)不支持军事/非人工对象

  • 一种解决方法是使用MemCacheStore,另一种方法是设置config.cache_classes=true。另请参见

    能否将代码和缓存配置与一些代码一起放置…pass:raw=>true选项在缓存读写方法中,我怀疑加载数据时出现问题。希望这能起作用。能否将代码和缓存配置与一些代码一起放置…pass:raw=>true选项放在缓存读写方法中,我怀疑加载数据时出现问题。希望这能奏效。