ruby-on-rails - 如何在 Redis 对象 gem 中使用 hash_key?

标签 ruby-on-rails ruby hash redis

我需要使用 redis-objects 将哈希存储到 Redis 中 gem 。从 README 中,他们有 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

但是 setter/getter 起作用了:

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

最佳答案

HashKey class 上使用更新方法:

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

阅读:

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

关于ruby-on-rails - 如何在 Redis 对象 gem 中使用 hash_key?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33460154/

相关文章:

ruby-on-rails - 为什么 pp-adaptive 不接受我的退款金额?

ruby-on-rails - rails : when to use self

ruby - Rails - 如何处理不存在的路由 ("No route matches [GET]")?

android - LoginWithFacebook 从 Android 应用程序

ruby 年哈希生成

javascript - 在javascript中找到具有公共(public)键的哈希数组的最大值

ruby-on-rails - 搜索如何使用 rails3 设置 mongo_mapper、设计、haml 的指南

ruby-on-rails - 如何在 Rails 6/Zeitwerk 中扩展 gem 类而不破坏代码重新加载?

ruby-on-rails - 我可以在 select_tag 中包含空白字段吗?

c - 如何在 Ruby C API 中获取 VALUE 的类