gpt4 book ai didi

mysql - rails 调试

转载 作者:行者123 更新时间:2023-11-29 06:46:04 25 4
gpt4 key购买 nike

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

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

首先,如果我拖尾日志,我得到的错误是:

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)) %>

在 Controller 中定义为

@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)。

最佳答案

基于@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)

并删除第 15 行的 .map(&:reverse)

如果你想让它真正独立于 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"]] }

关于mysql - rails 调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18638313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com