0

我需要扫描数组的每个值并根据其中一个字段中的值进行处理。下面是我的控制器类中的代码

 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 的初学者,已经阅读了很多教程,浏览了很多(尝试了很多不同的选项),仍然没有运气。请指教。谢谢。

4

1 回答 1

1
@tables.each do |table|
  # do something with each table.tabposition
  if table.tabposition == 'something'
    'foo'
  end
end
于 2013-08-27T13:21:23.277 回答