在我看来,我发送一个 ajax 请求来获取device_ports
特定的device
.
以前我用
def get_device_ports
if params[:id] != ''
@device_ports = Device.find(params[:id]).device_ports.all(:order => 'id ASC')
output = '<option value="">Select Device Port...</option>'
@device_ports.each do |device_port|
output = output + '<option value="' + device_port.id.to_s + '">' + device_port.name + '</option>'
end
render :text => output
else
render :text => '0'
end
end
哪个有效,但现在更改了我的查询,我得到了一个错误undefined method 'name' for [268, "test-1"]:Array
,268
并且test-1
是第一行结果的id
and 。name
这是我更新的代码:
def get_device_ports
if params[:id] != '' and params[:device_id] != ''
# @device_ports = Device.find(params[:id]).device_ports.all(:order => 'id ASC')
device_id = params[:device_id]
# Need a list of ports that aren't in use or are multiuse
@device_ports = ActiveRecord::Base.connection.execute('SELECT DISTINCT d.id, d.name FROM device_ports d LEFT OUTER JOIN circuits c ON c.physical_port_id = d.id WHERE (c.physical_port_id IS NULL AND d.device_id = ' + device_id + ') OR (d.multiuse = 1 AND d.device_id = ' + device_id + ') ORDER BY d.id ')
output = '<option value="">Select Device Port...</option>'
@device_ports.each do |device_port|
output = output + '<option value="' + device_port.id.to_s + '">' + device_port.name + '</option>'
end
render :text => output
else
render :text => '0'
end
end
我只是不确定为什么我会收到错误,我想这是微不足道的,但由于不同的NoMethodError
问题的数量很难找到答案。