rails,无法在视图中显示postgres数组计数结果

时间:2022-09-22 15:16:35

Rails 4.2.1 Ruby 2.0.0

Rails 4.2.1 Ruby 2.0.0

I want to display the results of a postgres array count query as a simple table in a rails view. The table should have 2 columns; deployed and count.

我想将postgres数组计数查询的结果显示为rails视图中的简单表。该表应该有2列;部署和统计。

I am able to successfully query postgres using the following SQL

我能够使用以下SQL成功查询postgres

select unnest(deployed), count(deployed) from current_customer_profiles group by current_customer_profiles.deployed;

The results of the postgres query are

postgres查询的结果是

deployed | count

on prem  | 4

saas     | 2

I have tried using select_rows and find_by_sql in rails:

我尝试在rails中使用select_rows和find_by_sql:

@deployment = CurrentCustomerProfile.connection.select_rows('select unnest(deployed) as deployed, count(deployed) as count from current_customer_profiles group by current_customer_profiles.deployed')

@deployment = CurrentCustomerProfile.find_by_sql(%q{select unnest(deployed) as deployed, count(deployed) as count from current_customer_profiles group by current_customer_profiles.deployed})

I have tried many different ways to get the data to display as a simple table in a rails view.

我尝试了许多不同的方法来将数据显示为rails视图中的简单表。

The problems that I have faced are:

我遇到的问题是:

  1. The string (deploy column) is shown as [] in the view instead of the value (interestingly the count displays fine, so the result looks like; [] 4 and [] 2)
  2. 字符串(部署列)在视图中显示为[]而不是值(有趣的是计数显示正常,因此结果看起来像; [] 4和[] 2)

  3. The results are returned as a string of the deployed column and count, i.e. "on prem 4" instead of two columns, on prem, 4
  4. 结果作为已部署列和计数的字符串返回,即“在前4”而不是两列,在前,4

Below are two view examples I have tried (in respective order of the two items above):

以下是我尝试的两个视图示例(按上述两个项目的相应顺序):

<% @deployment.each do |e| %>
    <%= e.deployed %> <%= e.count %>
<% end %>


<% @deployment.each do |b| %>
    <% b.each do |bb| %>
     <%= bb %>
        <% end %>
<% end %>

How can I display the results of a count of the items in a postgres array in a table in a rails view? This is my first post on stack overflow so please let me know if I should provide more information.

如何在rails视图的表中显示postgres数组中项目计数的结果?这是我关于堆栈溢出的第一篇文章,所以如果我应该提供更多信息,请告诉我。

3 个解决方案

#1


I was able to use this approach to the hash results

我能够将这种方法用于哈希结果

<% @deployment.each do |key, value| %>
  <%= key %> <%= value %>
<% end %>

#2


As query returns results in hashes so you would have to do it like this

当查询返回哈希值时,你必须这样做

 <% @deployment.each do |e| %>
   <%= e['deployed'] %> <%= e['count'] %>
 <% end %>

#3


Count is an attribute, try this like

Count是一个属性,试试这个就好了

 <% @deployment.each do |e| %>
   <%= e['deployed'] %> <%= e['count'] %>
 <% end %>

#1


I was able to use this approach to the hash results

我能够将这种方法用于哈希结果

<% @deployment.each do |key, value| %>
  <%= key %> <%= value %>
<% end %>

#2


As query returns results in hashes so you would have to do it like this

当查询返回哈希值时,你必须这样做

 <% @deployment.each do |e| %>
   <%= e['deployed'] %> <%= e['count'] %>
 <% end %>

#3


Count is an attribute, try this like

Count是一个属性,试试这个就好了

 <% @deployment.each do |e| %>
   <%= e['deployed'] %> <%= e['count'] %>
 <% end %>