Gaurav
Gaurav

Reputation: 535

Looping an array in Ruby

I need to scan each value of array and do the processing based on values in one of the fields. Below is the code in my controller class

 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"[email protected]).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"[email protected]).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"[email protected]).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"[email protected]).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"[email protected]).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"[email protected]).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"[email protected]).update_all(:tabstatus => 'displayed',:tabposition => 'position1')

    end
  end
end

I am getting @tables value as

=> [#<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>]

Now I need check tabposition field for each row fetched and then do the processing thereafter. I am getting error; undefined method tableno for nil:NilClass inside first If statment. Ideally it shouldn't go in the first IF. Seems like not getting table.tabposition value.

Sincere apologies if this sounds a basic question. I am a beginner in rails and have read many tutorials,browsed a lot(tried many different options), still no luck. Please advise.Thanks.

Upvotes: 0

Views: 69

Answers (1)

Dty
Dty

Reputation: 12273

@tables.each do |table|
  # do something with each table.tabposition
  if table.tabposition == 'something'
    'foo'
  end
end

Upvotes: 1

Related Questions