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.
I am able to successfully query postgres using the following SQL
select unnest(deployed), count(deployed) from current_customer_profiles group by current_customer_profiles.deployed;
The results of the postgres query are
deployed | count
on prem | 4
saas | 2
I have tried using select_rows and find_by_sql in rails:
@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.
The problems that I have faced are:
- 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)
- 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
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.