如果声明和/或破坏服务器

时间:2013-03-01 03:05:44

标签: ruby-on-rails ruby-on-rails-3.2

我有一个after_create语句,它使用模型创建嵌套属性。我正在过滤一周中的布尔字段,但是if语句冻结了服务器(没有错误)。

我在下面列出了相关代码。对于通知,星期六到星期六是数据库中的布尔字段。如果我删除所有过滤布尔字段的“和”子句,则表单完成,但我需要过滤掉工作日未选择的日期。

def new_visit  
  day = 0
  dates = (visit_date_start .. visit_date_end).count + 1
  while day <= dates
    date = visit_date_start + day
    day_of_week = date.strftime("%A").downcase
    if (day_of_week == 'sunday' and sunday == true) or (day_of_week == 'monday' and monday == true) or (day_of_week == 'tuesday' and tuesday == true) or (day_of_week == 'wednesday' and wednesday == true) or (day_of_week == 'thursday' and thursday == true) or (day_of_week == 'friday' and friday == true) or (day_of_week == 'saturday' and saturday == true)
      visits.create(:visit_price => visit_price, :visit_type => visit_type, :client_id => client_id, :visit_date => date)
      day += 1
    end
  end
end

更新

我查看了日志,偶尔,服务器会抛出如下错误。我相信这是一个sqlite锁定错误。对此更深入了解吗?

BusyException: cannot rollback transaction - SQL statements in progress: rollback transaction

1 个答案:

答案 0 :(得分:0)

解决方案是我需要添加一个“else”子句,否则该方法会等待下一步做什么。结果方法是:

def new_visit  
  day = 0
  dates = (visit_date_start .. visit_date_end).count + 1
  while day <= dates
    date = visit_date_start + day
    day_of_week = date.strftime("%A").downcase
    if sunday == true
    #if (day_of_week == 'sunday') or (day_of_week == 'monday') or (day_of_week == 'tuesday') or (day_of_week == 'wednesday') or (day_of_week == 'thursday') or (day_of_week == 'friday') or (day_of_week == 'saturday')
    #if ((day_of_week == 'sunday') and (sunday == true)) or ((day_of_week == 'monday') and (monday == true)) or ((day_of_week == 'tuesday') and (tuesday == true)) or ((day_of_week == 'wednesday') and (wednesday == true)) or ((day_of_week == 'thursday') and (thursday == true)) or ((day_of_week == 'friday') and (friday == true)) or ((day_of_week == 'saturday') and (saturday == true))
      visits.create(:visit_price => visit_price, :visit_type => visit_type, :client_id => client_id, :visit_date => date)
      day += 1
    else
      day += 1
    end
  end
end