1

我有一个 Rails 2 应用程序,在开发中一切正常,但在生产中,我的一个观点被打破了,我很难找出原因。

开发和生产之间的唯一区别是,出于某种原因,开发使用mysql2gem 而生产使用原始mysqlgem。

首先,如果我跟踪日志,我得到的错误是:

ActionView::TemplateError (undefined method 'map' for #<Mysql::Result:0xb5ce7844>) on line #15 of app/views/logical_interface/create.rhtml:

第 15 行是

<%= select_tag 'logical_interface[vlan_id]', options_for_select(@vlan_numbers.map(&:reverse)) %>

在控制器中定义为

@pop_id = session[:pop_id]

@vlan_numbers = ActiveRecord::Base.connection.execute("SELECT pop_vlans.id, vlan_number FROM pop_vlans WHERE (pop_id = '" + @pop_id.to_s + "' AND vlan_number = 'Untagged') OR pop_vlans.id NOT IN (SELECT logical_interfaces.vlan_id FROM logical_interfaces) AND pop_id = " + @pop_id.to_s)

我检查了@pop_id它是否失败,因为它是 nil 等,但打印出来显示正确的值。

如果我删除第 15 行并添加<%= @vlan_numbers.inspect %>所有我得到的是#,如果我这样做了,debug @vlan_numbers#<Mysql::Result:0xb5db1298>真的很难过。

我看了看是否有任何东西mysql2不在,mysql但我能找到的mysql2只是总体上更好。

有任何想法吗?

更新

@vlan_numbers = ActiveRecord::Base.connection.select_all("SELECT pop_vlans.id, vlan_number FROM pop_vlans WHERE (pop_id = '" + @pop_id.to_s + "' AND vlan_number = 'Untagged') OR pop_vlans.id NOT IN (SELECT logical_interfaces.vlan_id FROM logical_interfaces) AND pop_id = " + @pop_id.to_s).collect { |r| r['vlan_number'] }

我尝试按照上面的建议使用我的 mbratch (答案现在已被删除),但这有点奇怪,因为Untagged添加到列表中的选项有它的文本,因此它是deggatnU. 我认为选择值也会发生同样的情况(看起来它们只是文本而不是id表中的实际值)。

4

2 回答 2

2

在@Vimsha 给出的基础上,您可以尝试这个将创建一个数组(列表)[id, vlan_number]对:

@vlan_numbers = ActiveRecord::Base.connection.select_all("SELECT vlan_number, pop_vlans.id FROM pop_vlans WHERE (pop_id = '" + @pop_id.to_s + "' AND vlan_number = 'Untagged') OR pop_vlans.id NOT IN (SELECT logical_interfaces.vlan_id FROM logical_interfaces) AND pop_id = " + @pop_id.to_s).map(&:values)

并摆脱.map(&:reverse)在线15。

如果你想让它真正独立于SELECT产生的顺序,那么稍微长一点:

@vlan_numbers = ActiveRecord::Base.connection.select_all("SELECT vlan_number, pop_vlans.id FROM pop_vlans WHERE (pop_id = '" + @pop_id.to_s + "' AND vlan_number = 'Untagged') OR pop_vlans.id NOT IN (SELECT logical_interfaces.vlan_id FROM logical_interfaces) AND pop_id = " + @pop_id.to_s).map { |r| [r["vlan_number"], r["id"]] }
于 2013-09-05T14:17:29.653 回答
0

您收到此错误是因为 @vlan_numbers 是 Mysql::Result 对象而不是数组。如果要获取数组,请执行此操作

@vlan_numbers = ActiveRecord::Base.connection.select_all("SELECT pop_vlans.id, vlan_number FROM pop_vlans WHERE (pop_id = '" + @pop_id.to_s + "' AND vlan_number = 'Untagged') OR pop_vlans.id NOT IN (SELECT logical_interfaces.vlan_id FROM logical_interfaces) AND pop_id = " + @pop_id.to_s)
于 2013-09-05T14:01:54.660 回答