Store accessor for nested JSON

后端 未结 2 941
甜味超标
甜味超标 2021-02-12 11:28

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 pertainin

2条回答
  •  说谎
    说谎 (楼主)
    2021-02-12 12:05

    I was looking for the same thing. As @twonegatives indicated, store_accessor won't help us. But I did find the #dig method to work pretty well for getting the data. So...

    #somewhere in Organization model
    def api_key
      integrations.dig("mailchimp", "api_key")
    end
    
    def username
      integrations.dig("sendgrid", "username")
    end
    

提交回复
热议问题