我需要扫描数组的每个值,并根据其中一个字段中的值进行处理。 下面是我的控制器类中的代码
def index
@tables = Table.select("tablabel,tabposition").where(:tabstatus => "displayed")
if @tables
@tables.each do |table|
if (table.tabposition == "position1")
@orders1 = List.select("itemname,tableno,SUM(quantity) as quantity").group("itemname").where(:tableno => table.tablabel)
Table.where(:tablabel => "Table"+@orders1.first.tableno).update_all(:tabstatus => 'displayed',:tabposition => 'position1')
else if (table.tabposition == "position2")
@orders2 = List.select("itemname,tableno,SUM(quantity) as quantity").group("itemname").where(:tableno => table.tablabel)
Table.where(:tablabel => "Table"+@orders2.first.tableno).update_all(:tabstatus => 'displayed',:tabposition => 'position2')
else if (table.tabposition == "position3")
@orders3 = List.select("itemname,tableno,SUM(quantity) as quantity").group("itemname").where(:tableno => table.tablabel)
Table.where(:tablabel => "Table"+@orders3.first.tableno).update_all(:tabstatus => 'displayed',:tabposition => 'position3')
else if (table.tabposition == "position4")
@orders4 = List.select("itemname,tableno,SUM(quantity) as quantity").group("itemname").where(:tableno => table.tablabel)
Table.where(:tablabel => "Table"+@orders4.first.tableno).update_all(:tabstatus => 'displayed',:tabposition => 'position4')
else if (table.tabposition == "position5")
@orders5 = List.select("itemname,tableno,SUM(quantity) as quantity").group("itemname").where(:tableno => table.tablabel)
Table.where(:tablabel => "Table"+@orders5.first.tableno).update_all(:tabstatus => 'displayed',:tabposition => 'position5')
else if (table.tabposition == "position6")
@orders6 = List.select("itemname,tableno,SUM(quantity) as quantity").group("itemname").where(:tableno => table.tablabel)
Table.where(:tablabel => "Table"+@orders6.first.tableno).update_all(:tabstatus => 'displayed',:tabposition => 'position6')
end
end
end
end
end
end
end
else
@orders1 = List.select("itemname,tableno,SUM(quantity) as quantity").group("itemname")
Table.where(:tablabel => "Table"+@orders1.first.tableno).update_all(:tabstatus => 'displayed',:tabposition => 'position1')
end
end
end
我的@tables值为
=> [#<Table tablabel: "Table03", tabposition: nil>, #<Table tablabel: "Table06",
tabposition: nil>, #<Table tablabel: "Table07", tabposition: nil>, #<Table tabl
abel: "Table08", tabposition: nil>, #<Table tablabel: "Table09", tabposition: ni
l>, #<Table tablabel: "Table10", tabposition: nil>]
现在我需要为每个提取的行检查tabposition
字段,然后再进行处理。我收到了错误; undefined method tableno for nil:NilClass
在第一个if里面。理想情况下,它不应该进入第一个IF。好像没有得到table.tabposition值。
如果这听起来是一个基本问题,真诚的道歉。我是rails的初学者,已经阅读了很多教程,浏览了很多(尝试了很多不同的选项),但仍然没有运气。请指教。谢谢。
答案 0 :(得分:1)
@tables.each do |table|
# do something with each table.tabposition
if table.tabposition == 'something'
'foo'
end
end