Ruby on rails 在Ruby中循环数组

Ruby on rails 在Ruby中循环数组,ruby-on-rails,Ruby On Rails,我需要扫描数组的每个值,并根据其中一个字段中的值进行处理。 下面是我的控制器类中的代码 def index @tables = Table.select("tablabel,tabposition").where(:tabstatus => "displayed") if @tables @tables.each do |table| if (table.tabposition == "position1") @orde

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

 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
字段,然后再进行处理。我犯了错误<代码>nil的未定义方法tableno:NilClass在第一个If语句中。理想情况下,它不应该在第一个如果。似乎无法获取table.tabposition值


如果这听起来是一个基本问题,真诚的道歉。我是rails的初学者,已经阅读了很多教程,浏览了很多(尝试了很多不同的选项),但仍然运气不佳。请给出建议。谢谢。

如果你能说出你想要实现的目标,我认为整个代码可以减少到5行。第3行,是
@table
还是
@tables
?第4行的
表格如何?基本上,我需要在视图中用表格数据(ItemName和quantity)动态填充6个不同的部分(6个表格)。为了实现这一点,我首先获取所有表,并检查它们是否已经在屏幕上。where子句中的tabstatus就是这样做的。然后我需要确定填充表的位置(共6个)。如果我有该位置,刷新时它将显示在同一位置。因此,我正在检查每个tablabel的tabposition。在
index
方法的前三行中,您使用
@tables
@table
tables
,它们是指同一个变量吗?感谢您指出这一点。我已经改正了。到处都是桌子。
@tables.each do |table|
  # do something with each table.tabposition
  if table.tabposition == 'something'
    'foo'
  end
end