嵌套JSON的商店访问器 [英] Store accessor for nested JSON

查看:48
本文介绍了嵌套JSON的商店访问器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组织"模型,该模型存储与组织有关的所有信息. JSONB类型的字段名为集成",用于存储与组织拥有的所有外部服务集成有关的信息.

I have the model "Organization" that stores all information related to an organization. There is a field of type JSONB named "integrations" that stores information pertaining to all external service integrations that an organization has.

如何使用商店访问器访问嵌套JSON中存储的信息,例如:

How do I use store accessors to access information stored in nested JSON, for example :

{
 "mailchimp": {"api_key":"vsvsvef", "list_id":"12345"},
 "sendgrid" : {"username":"msdvsv", "password":"123456"}
}

我知道我可以使用这样的商店访问器来访问mailchimp:

I know I can access mailchimp with store accessors like this :

store_accessor :integrations, :mailchimp

如何轻松访问mailchimp的api_key?

How can I easily access the api_key of mailchimp?

推荐答案

很抱歉,store_accessor不允许您访问嵌套键.原因是store_accessor基本上只是定义getter和setter方法的快捷方式:

You're right, unfortunately store_accessor does not allow you to access nested keys. The reason is that store_accessor is basically just a shortcut which defines getter and setter methods:

# here is a part of store_accessor method code, you can take a look at
# full implementation at
# http://apidock.com/rails/ActiveRecord/Store/ClassMethods/store_accessor
_store_accessors_module.module_eval do
  keys.each do |key|
    # here we define a setter for each passed key
    define_method("#{key}=") do |value|
      write_store_attribute(store_attribute, key, value)
    end

    # and here goes the getter
    define_method(key) do
      read_store_attribute(store_attribute, key)
    end
  end
end

因此,您在这里的选择是:

So, your options here are:

  1. 要手动实现自己的一组getter和setter方法:

  1. To implement your own set of getter and setter methods manually:

# somewhere in your model
def mailchimp_api_key
  self.mailchimp["api_key"]
end

def mailchimp_api_key= value
  self.mailchimp["api_key"] = value
end

这解决了一个问题,但是您必须为每个嵌套属性重复编写很多这样的内容.

This solves a problem, but you'd have to write lots of this repeatedly for each of nested attributes.

要在ActiveRecord::Store::ClassMethods模块内编写自己的帮助程序方法,该方法将为传入的属性集动态定义相同的setter和getter方法.您必须采用Rails ,并向其添加其他哈希键迭代.不确定这是否会很容易,但是作为一个宝石来共享肯定会很有趣.

To write your own helper method inside of ActiveRecord::Store::ClassMethods module which would define the same setter and getter methods dynamically for the set of attributes you pass in. You'd have to take the basic implementation of Rails store_accessor and add an additional hash keys iteration to it. Not sure if this is going to be an easy one, but would definitely be interesting to see shared as a gem.

离开Rails本身,并使用带有纯SQL代码的postgres json类型支持的功能.例如,您可以使用以下内容访问api_key属性:

Leave Rails itself and use the power of postgres json type support with some pure SQL code. For example, you can access api_key attribute with something like that:

SELECT integrations->'mailchimp'->>'api_key' as mailchimp_api_key FROM your_table_name;

有关postgres json查询的更多信息,请此处.

More on postgres json queries can be found here.

这篇关于嵌套JSON的商店访问器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆