[英]How can I iterate through an array in rails view?
I did a query in MySql but is working in Rails and mysql2 gem. 我在MySql中进行了查询,但是在Rails和mysql2 gem中工作。
Here is the information: 这是信息:
http://sqlfiddle.com/#!2/9adb8/6
The query is working fine without problems and showing this result: 查询工作正常没有问题,并显示此结果:
UNIT V1 A1 N1 V2 A2 N2 V3 A3 N3 V4 A4 N4 V5 A5 N5
LIFE 2 0 0 1 2 0 0 0 0 0 0 0 0 0 0
ROB 0 1 0 0 1 2 0 0 0 0 0 0 0 0 0
-Installed mysql2 gem for rails 2.3.8 - 为rails 2.3.8安装了mysql2 gem
gem install mysql2 -v0.2.6
-Created the controller: - 控制器:
class PolicyController < ApplicationController
def index
@result = ActiveRecord::Base.connection.execute("select distinct @sql := concat('SELECT pb.name as unit,',group_concat(concat('SUM(CASE WHEN p.state =0 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS v',id,',SUM(CASE WHEN p.state =1 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS a',id,',SUM(CASE WHEN p.state =2 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS n',id)),' FROM cia_ensures ce LEFT JOIN policies p on ce.id = p.cia_ensure_id INNER JOIN policy_business_units pb ON pb.id = p.policy_business_unit_id INNER JOIN comercial_areas ca ON ca.id = pb.comercial_area_id AND ca.id=1 Group by p.policy_business_unit_id') from cia_ensures where id in(1,2,3,4,5);")
@result2 = ActiveRecord::Base.connection.execute("prepare stmt from @sql;")
@result3 = ActiveRecord::Base.connection.execute("execute stmt;")
end
end
Here is the log: 这是日志:
SQL (0.9ms) select distinct @sql := concat('SELECT pb.name as unit,',group_concat(concat('SUM(CASE WHEN p.state =0 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS v',id,',SUM(CASE WHEN p.state =1 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS a',id,',SUM(CASE WHEN p.state =2 AND ce.id=',id,' THEN 1 ELSE 0 END ) AS n',id)),' FROM cia_ensures ce LEFT JOIN policies p on ce.id = p.cia_ensure_id INNER JOIN policy_business_units pb ON pb.id = p.policy_business_unit_id INNER JOIN comercial_areas ca ON ca.id = pb.comercial_area_id AND ca.id=1 Group by p.policy_business_unit_id') from cia_ensures where id in(1,2,3,4,5);
SQL (0.9ms) prepare stmt from @sql;
SQL (0.2ms) execute stmt;
Here is the view (is working fine and without problems but seems to be too long write several times the same code) 这是视图(工作正常,没有问题,但似乎太长写了几次相同的代码)
<table>
<% @result3.each do |policy| %>
<tr>
<td><%= policy[0] %></td>
<td><%= policy[1] %></td>
<td><%= policy[2] %></td>
<td><%= policy[3] %></td>
<td><%= policy[4] %></td>
<td><%= policy[5] %></td>
...
</tr>
<%end%>
</table>
I tried to use inspect but it shows all the information in one td and not on each td: 我尝试使用inspect但是它显示了一个td中的所有信息而不是每个td:
<% @result3.each do |policy| %>
<tr>
<td align="center"><%= policy.inspect %></td>
</tr>
<%end%>
How can I do to show all this without writing lots of lines? 如何在不写大量行的情况下显示所有这些内容?
Is it possible to make this in one line? 有可能在一行中做到这一点吗? without writing <%= policy[#NUMBER] %>
无需编写<%= policy [#NUMBER]%>
Please somebody can help me with this? 请有人帮我这个吗?
I will really appreciate it. 我真的很感激。
Hmmm, might have missunderstood your question since it looks really simple, are you trying to shorten this? 嗯,可能会误解你的问题,因为它看起来很简单,你想缩短这个吗?
Change: 更改:
<td><%= policy[0] %></td>
<td><%= policy[1] %></td>
<td><%= policy[2] %></td>
<td><%= policy[3] %></td>
<td><%= policy[4] %></td>
<td><%= policy[5] %></td>
To: 至:
<% policy.each do |p| %>
<td><%= p %></td>
<% end %>
Do this 做这个
<% @result3.each do |policy| %>
<tr>
<% policy.each { |p| raw "<td>#{p}</td>" } %>
</tr>
<%end%>
Just iterate over each collection in each policy
object. 只需遍历每个
policy
对象中的每个集合。
<table>
<% @result3.each do |policy| %>
<tr>
<%policy.each do |item| %>
<td><%= item %></td>
<%end%>
</tr>
<%end%>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.