【问题标题】:How to use hash_key in Redis Objects gem?如何在 Redis Objects gem 中使用 hash_key?
【发布时间】:2016-02-01 07:19:53
【问题描述】:

我需要使用 redis-objects gem 将哈希存储到 Redis 中。从自述文件中,他们有hash_key。但是没有这方面的例子。

我收到一个错误:

NoMethodError: undefined method `fantasy_points_details=' for #<InningPlayer:0x007f820adb68a0>

当我这样做时:

 inning_player_instance.fantasy_points_details = {a: 1}

对于这样的模型:

class InningPlayer < ActiveRecord::Base
    include Redis::Objects
    hash_key :fantasy_points_details
end

但吸气剂有效:

 > inning_player_instance.fantasy_points_details
 => #<Redis::HashKey:0x007f820adf4470>

【问题讨论】:

    标签: ruby-on-rails ruby hash redis


    【解决方案1】:

    使用HashKey class上的更新方法:

    >  inning_player_instance.fantasy_points_details.update({"a"=>"3", "c"=>"4"})
    => "OK"
    

    阅读:

    > ip.fantasy_points_details.all
    => {"a"=>"3", "c"=>"4"}
    

    【讨论】:

    • 另外,您必须执行inning_player_instance.fantasy_points_details["a"] = 1 才能一次设置和获取一个哈希值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-01
    • 2019-11-29
    • 2012-05-20
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多