stop a loop if model changes

50 views Asked by At

I'm trying to exit from a loop if model changes.

having the status attribute:

@model.status = 'started'

(0..100).each do |i|
  return if status == 'stopped'
  sleep(10)  
  # my taks...
end

the problem is, once is started changes in the model are not reflected within the loop, any ideas?

1

There are 1 answers

1
Brad On

You would need to query the status inside the loop so that it gets an updated status.

(0..100).each do |i|
  status = Model.find(:id).status
  return if status == 'stopped'
  sleep(10)  
  # my taks...
end