我在MySql中进行了查询但是没有使用Rails,所以我安装了mysql2 gem。
以下是信息:
http://sqlfiddle.com/#!2/9adb8/6
查询正常运行没有问题并显示此结果:
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
- 为rails 2.3.8安装了mysql2 gem
gem install mysql2 -v0.2.6
- 处理控制器:
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
- 处理观点:
<% @result.each do |policy| %>
<%= policy.try(:unit) %>
<%= policy.try(:v1) %>
<% end %>
这是日志:
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;
我在视图中遇到了这个错误:
undefined method `unit' for #<Array:0x7f0e668cbf88>
尝试将我的观点中的值更改为:
<% @result2.each do |policy| %>
<%= policy.try(:unit) %>
<%= policy.try(:v1) %>
<% end %>
得到了这个错误:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
再次尝试将我的观点中的值更改为:
<% @result3.each do |policy| %>
<%= policy.try(:unit) %>
<%= policy.try(:v1) %>
<% end %>
得到了这个错误:
undefined method `unit' for #<Array:0x7f0e66b67aa8>
再次尝试将我的观点中的值更改为:
<% @result3.each do |policy| %>
<%= policy.try(:unit) %>
<%= policy.try(:v1) %>
<% end %>
我无法显示或获取查询中的值,是否有显示值的方法?
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
根据朋友的说法,这段代码可行,但如何在不写大量行的情况下显示所有这些内容?
<% @result3.each do |policy| %>
<%= policy[0] %>
<%= policy[1] %>
<%= policy[2] %>
<%= policy[3] %>
<%= policy[4] %>
<%= policy[5] %>
<%= policy[6] %>
<%= policy[7] %>
....
<%end%>
我花了一个月没有找到答案,仍然无法获得价值。
请有人帮我这个吗?
我真的很感激。
答案 0 :(得分:1)
我认为@result3
是您想要的,因为它正在执行查询。
undefined method `unit' for #<Array:0x7f0e66b67aa8>
这告诉你try
发现该对象是非零的,并试图在该对象上调用#unit
方法。这似乎不太适用于MySQL结果集。
我从来没有使用过mysql2,但是一个快速谷歌显示它的结果对象是一个类似哈希的对象,其中包含列名的键或者像数组一样的直接数字索引,所以尝试这样的事情:
<% @result3.each do |policy| %>
<%= policy['unit'] %>
<%= policy['v1'] %>
<% end %>
或
<% @result3.each do |policy| %>
<%= policy['UNIT'] %>
<%= policy['V1'] %>
<% end %>
或
<% @result3.each do |policy| %>
<%= policy[0] %>
<%= policy[1] %>
<% end %>
如果这不起作用,至少可以尝试一下,看看你会得到什么:
<% @result3.each do |policy| %>
<%= policy.inspect %>
<% end %>