0

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.

6
  • If you can tell what you are trying to achieve, I think the whole code can be cut down to 5 lines. Commented Aug 27, 2013 at 13:24
  • 2
    in line #3, is it @table or @tables? and what about the tables of line #4? Commented Aug 27, 2013 at 13:33
  • basically I need to populate 6 different sections(for 6 tables) with Table data(itemnames and quantity) dynamically in my view. To achieve this initially I fetch all tables and check if they are already on screen or not. tabstatus in where clause does that. Then I need to determine the position (out of 6) in which the table is populated. If I have the position, on refresh it will be displayed on same position. So I am checking tabposition for each tablabel. Commented Aug 27, 2013 at 13:34
  • 1
    in the first three lines of your index method you use @tables, @table and tables, are these meant to be the same variable? Commented Aug 27, 2013 at 13:43
  • Thanks for pointing this. I have corrected that. It's @tables everywhere. Commented Aug 27, 2013 at 13:51

1 Answer 1

1
@tables.each do |table|
  # do something with each table.tabposition
  if table.tabposition == 'something'
    'foo'
  end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.