如何在Rails中解析和显示hstore键

如何在Rails中解析和显示hstore键

本文介绍了如何在Rails中解析和显示hstore键/值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据类型为postgres hstore,它存储一个json字符串。

I'ved got a data type that is postgres hstore that stores a json string.

我希望能够解析并在rails上显示键/值

I want to be able to parse and display the key/values on rails html page.

以某种方式,我只是不知道如何在数据字段上运行解析并显示字符串中列出的每个键/值。

Somehow, I just do not know how to run a parse on the data field and display each one of key/value listed in the string.

<% @payment_accounts.each do |payment_account| %>
  <tr>

    <td><%= payment_account.name %></td>
    <td><%= payment_account.company %></td>
    <td><%= payment_account.data %></td>        <-- this is the hstore json string
     <td><%= Json.parse(payment_account.data) %></td>   <-- this is an error, just to show

  </tr>
<% end %>

示例为payment_account.data包含{ hello => world, great = >工作}

example would be payment_account.data contains {"hello"=>"world", "great"=>"job"}

这是index.html.erb代码。

here is the index.html.erb code.https://gist.github.com/axilaris/9174206

我应该怎么做才能做到这一点?就是解析hstore字符串以在rails中显示查询结果吗?

what should i do to accomplish this ? that is parsing hstore string to display a query result in rails ?

推荐答案

您可以像访问数组一样访问数据:

You can access the data like an array:

<%= payment_account.data['hello'] %>

这将显示世界

这篇关于如何在Rails中解析和显示hstore键/值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 23:17