【问题标题】:Sinatra app (potentially) calling database twiceSinatra 应用程序(可能)调用数据库两次
【发布时间】:2014-05-05 21:53:23
【问题描述】:

我遇到了一个相当奇怪的问题。我在其中一条路线中进行了以下测试:

error 404 unless Agreement.find_by(agreement_id: params[:agreement_id])

这是一个active record 模型,我只是使用提供的find_by 方法。当我检查日志时,这是输出:

D, [2014-03-25T09:40:06.207120 #5956] DEBUG -- :   Agreement Load (0.1ms)  SELECT "agreements".* FROM "agreements" WHERE "agreements"."agreement_id" = '<id>' LIMIT 1
127.0.0.1 - - [25/Mar/2014:09:40:06 +0100] "HEAD /agreements/<id> HTTP/1.1" 204 0.0142
D, [2014-03-25T09:40:06.216059 #5956] DEBUG -- :   Agreement Load (0.2ms)  SELECT "agreements".* FROM "agreements" WHERE "agreements"."agreement_id" = '<id>' LIMIT 1

所以,在我看来,发生了两件事之一:数据库被调用 两次(不好)或者它只是被记录了两次(但是,这似乎不是这种情况,因为查询显然不是以相同的速度运行(0.1ms 对 0.2ms))。

任何帮助将不胜感激。

【问题讨论】:

    标签: ruby activerecord sinatra rack


    【解决方案1】:

    事实证明,该应用程序确实被调用了两次,但这是由于中间件代码有问题,它调用了两次 @app.call

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多