Loop in Ruby on Rails running twice -


i'm going crazy. each-loop in rails runs twice each collection item. have no clue why.

    locations = location.all      locations.each |loc|        # delay not overload le webservice        sleep 1.0         if [... webservice call - runs fine ... ]          # score calculation - v0.1         score = [... formula calculate score ...]          score_total = score + loc.score_total         score_count = loc.score_count + 1          # update current record fresh values webservice             loc.update(:temp1 => temp[0],             :temp2 => temp[1],             :temp3 => temp[2],             :temp4 => temp[3],             :temp5 => temp[4],              :condition1 => condition[0],              :condition2 => condition[1],              :condition3 => condition[2],              :condition4 => condition[3],              :condition5 => condition[4],              :score_yesterday => loc.score_now,              :score_now => score,              :score_total => score_total,              :score_count => score_count)       end     end 

count jumps in 2-times steps

some variables want update within each iteration behind

log:

started "/loc" 127.0.0.1 @ 2014-02-01 13:37:22 +0100 processing whitelabelscontroller#loc html   [1m[36mwhitelabelwsd load (0.6ms)[0m  [1mselect "whitelabel_wsds".* "whitelabel_wsds"[0m   [1m[35m (0.3ms)[0m  begin   [1m[36msql (4.4ms)[0m  [1mupdate "whitelabel_wsds" set "temp1" = $1, "temp2" = $2, "temp3" = $3, "temp4" = $4, "temp5" = $5, "condition1" = $6, "condition2" = $7, "condition3" = $8, "condition4" = $9, "condition5" = $10, "score_now" = $11, "score_total" = $12, "score_count" = $13, "updated_at" = $14 "whitelabel_wsds"."id" = 2[0m  [["temp1", 17], ["temp2", 15], ["temp3", 13], ["temp4", 14], ["temp5", 13], ["condition1", "patchy rain nearby"], ["condition2", "cloudy "], ["condition3", "sunny"], ["condition4", "light rain shower"], ["condition5", "sunny"], ["score_now", 306], ["score_total", 10934], ["score_count", 48], ["updated_at", sat, 01 feb 2014 12:37:23 utc +00:00]]   [1m[35m (264.2ms)[0m  commit   [1m[36m (0.2ms)[0m  [1mbegin[0m   [1m[35msql (0.8ms)[0m  update "whitelabel_wsds" set "temp1" = $1, "temp2" = $2, "temp3" = $3, "temp4" = $4, "temp5" = $5, "condition1" = $6, "condition2" = $7, "condition3" = $8, "condition4" = $9, "condition5" = $10, "score_now" = $11, "score_total" = $12, "score_count" = $13, "updated_at" = $14 "whitelabel_wsds"."id" = 3  [["temp1", 16], ["temp2", 11], ["temp3", 16], ["temp4", 13], ["temp5", 17], ["condition1", "cloudy "], ["condition2", "sunny"], ["condition3", "cloudy "], ["condition4", "sunny"], ["condition5", "partly cloudy "], ["score_now", 391], ["score_total", 22791], ["score_count", 51], ["updated_at", sat, 01 feb 2014 12:37:25 utc +00:00]]   [1m[36m (1.3ms)[0m  [1mcommit[0m   [1m[35m (0.1ms)[0m  begin   [1m[36msql (1.4ms)[0m  [1mupdate "whitelabel_wsds" set "temp1" = $1, "temp2" = $2, "temp3" = $3, "temp4" = $4, "temp5" = $5, "condition1" = $6, "condition2" = $7, "score_now" = $8, "score_total" = $9, "score_count" = $10, "updated_at" = $11 "whitelabel_wsds"."id" = 1[0m  [["temp1", 21], ["temp2", 22], ["temp3", 21], ["temp4", 22], ["temp5", 23], ["condition1", "partly cloudy "], ["condition2", "partly cloudy "], ["score_now", 961], ["score_total", 93375], ["score_count", 61], ["updated_at", sat, 01 feb 2014 12:37:26 utc +00:00]]   [1m[35m (0.5ms)[0m  commit   rendered whitelabels/loc.html.erb within layouts/whitelabel (0.1ms) completed 200 ok in 4318ms (views: 6.6ms | activerecord: 277.8ms)   started "/loc" 127.0.0.1 @ 2014-02-01 13:37:26 +0100 processing whitelabelscontroller#loc html   [1m[36mwhitelabelwsd load (0.7ms)[0m  [1mselect "whitelabel_wsds".* "whitelabel_wsds"[0m   [1m[35m (0.2ms)[0m  begin   [1m[36msql (0.7ms)[0m  [1mupdate "whitelabel_wsds" set "score_yesterday" = $1, "score_total" = $2, "score_count" = $3, "updated_at" = $4 "whitelabel_wsds"."id" = 2[0m  [["score_yesterday", 306], ["score_total", 11240], ["score_count", 49], ["updated_at", sat, 01 feb 2014 12:37:27 utc +00:00]]   [1m[35m (0.6ms)[0m  commit   [1m[36m (0.2ms)[0m  [1mbegin[0m   [1m[35msql (0.6ms)[0m  update "whitelabel_wsds" set "score_yesterday" = $1, "score_total" = $2, "score_count" = $3, "updated_at" = $4 "whitelabel_wsds"."id" = 3  [["score_yesterday", 391], ["score_total", 23182], ["score_count", 52], ["updated_at", sat, 01 feb 2014 12:37:29 utc +00:00]]   [1m[36m (0.5ms)[0m  [1mcommit[0m   [1m[35m (0.2ms)[0m  begin   [1m[36msql (0.5ms)[0m  [1mupdate "whitelabel_wsds" set "score_yesterday" = $1, "score_total" = $2, "score_count" = $3, "updated_at" = $4 "whitelabel_wsds"."id" = 1[0m  [["score_yesterday", 961], ["score_total", 94336], ["score_count", 62], ["updated_at", sat, 01 feb 2014 12:37:30 utc +00:00]]   [1m[35m (0.5ms)[0m  commit   rendered whitelabels/loc.html.erb within layouts/whitelabel (0.1ms) completed 200 ok in 3598ms (views: 4.7ms | activerecord: 4.5ms) 

was going write comment, clearer here:

i'd imagine there 3 potential causes of problem:

  1. perhaps update saving data incorrectly / before & after webservice call
  2. your webservice may taking long
  3. perhaps else messing-up?

update_attributes

having looked on .update & .update_attributes methods, looks replace .update .update_attributes

although difference escapes me, use update_attributes time - need here:

 loc.update_attributes(             :temp1 => temp[0],             :temp2 => temp[1],             :temp3 => temp[2],             :temp4 => temp[3],             :temp5 => temp[4],              :condition1 => condition[0],              :condition2 => condition[1],              :condition3 => condition[2],              :condition4 => condition[3],              :condition5 => condition[4],              :score_yesterday => loc.score_now,              :score_now => score,              :score_total => score_total,              :score_count => score_count   ) 

webservice

another issue may webservice call taking long

any external dependencies carry added latency, may need factor process

currently, don't have logic determine whether call successful or not. explain app's increasing count without other items (it doesn't have access new data):

locations.each |loc|      if [... webservice call - runs fine ... ]          # score calculation - v0.1         score = [... formula calculate score ...]          score_total = score + loc.score_total          # update current record fresh values webservice             loc.update_attributes(:temp1 => temp[0],                 :temp2 => temp[1],                 :temp3 => temp[2],                 :temp4 => temp[3],                 :temp5 => temp[4],                  :condition1 => condition[0],                  :condition2 => condition[1],                  :condition3 => condition[2],                  :condition4 => condition[3],                  :condition5 => condition[4],                  :score_yesterday => loc.score_now,                  :score_now => score,                  :score_total => score_total         )          loc.increment!(:score_count)      end  end 

.increment! method ;)


logs

lots of these types of error can explained logs

the logs detail calls being made & when; allowing see what's happening


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -