0

我有一个使用模型创建嵌套属性的 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
4

1 回答 1

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 
于 2013-03-05T03:17:23.803 回答