首页 \ 问答 \ 关系随机引发ActiveRecord :: RecordNotFound(Relation randomly raises ActiveRecord::RecordNotFound)

关系随机引发ActiveRecord :: RecordNotFound(Relation randomly raises ActiveRecord::RecordNotFound)

偶尔尝试获取特定用户的订单记录时,会引发ActiveRecord::RecordNotFound

这里要注意一些事情。

  • 访问/orders/:id时会引发错误,但不是所有用户都会引发错误。 我们跟踪已完成的订单(意味着您最终在订单页面上),大约50%获得404.请注意,我们谈论的是50%的用户,而不是请求。 如果它为特定用户的订单显示404一次,则它将始终显示404。
  • 记录存在,因为可以使用控制器中记录的相同数据通过控制台访问该记录。
  • 重新部署应用程序时问题消失。

问题是什么?

我正在运行rails 4.2.0。

class OrdersController < ApplicationController
  #
  # GET /orders/:id
  #
  def show
    Rails.logger.info "current_user=#{current_user.id}, params[:id]=#{params[:id]}"
    @order = current_user.orders.find(params[:id])
  end
end


class ApplicationController < ActionController::Base
  def current_user
    @_current_user ||= User.find_by_id(cookies.signed[:uid])
  end
end

class User < ActiveRecord::Base
  has_many :subscriptions
  has_many :orders, through: :subscriptions
end

class Order < ActiveRecord::Base
  has_one :user, through: :subscription
end

class Subscription < ActiveRecord::Base
  belongs_to :user
end

这是日志输出

[User#id=2454266]   Parameters: {"id"=>"1553"}
[User#id=2454266] current_user=2454266, params[:id]=1553 <==== Rails.logger.info
[User#id=2454266] Completed 404 Not Found in 240ms
[User#id=2454266]
ActiveRecord::RecordNotFound (Couldn't find Order with 'id'=1553):
  app/controllers/orders_controller.rb:6:in `show'

在控制台中运行User.find(2454266).orders.find(1553)

另请注意,可以跳过关系并直接转到订单模型,就像这样

class OrdersController < ApplicationController
  #
  # GET /orders/:id
  #
  def show
    @order = Order.find(params[:id])
  end
end

Once in a while when trying to fetch an order record for a particular user, a ActiveRecord::RecordNotFound is raised.

Some things to note here.

  • The error is raised when visiting /orders/:id, but not for all users. We track completed orders (meaning that you end up on a orders page) and around 50% gets a 404. Note that we're talking about 50% of the users, not the requests. If it displays 404 once for an order for a particular user, it will always display a 404.
  • The record exists as it can be accessed via the console using the same data that's being logged in the controller.
  • The problem disappears when re-deploying the application.

What could the problem be?

I'm running rails 4.2.0.

class OrdersController < ApplicationController
  #
  # GET /orders/:id
  #
  def show
    Rails.logger.info "current_user=#{current_user.id}, params[:id]=#{params[:id]}"
    @order = current_user.orders.find(params[:id])
  end
end


class ApplicationController < ActionController::Base
  def current_user
    @_current_user ||= User.find_by_id(cookies.signed[:uid])
  end
end

class User < ActiveRecord::Base
  has_many :subscriptions
  has_many :orders, through: :subscriptions
end

class Order < ActiveRecord::Base
  has_one :user, through: :subscription
end

class Subscription < ActiveRecord::Base
  belongs_to :user
end

Here's the log output

[User#id=2454266]   Parameters: {"id"=>"1553"}
[User#id=2454266] current_user=2454266, params[:id]=1553 <==== Rails.logger.info
[User#id=2454266] Completed 404 Not Found in 240ms
[User#id=2454266]
ActiveRecord::RecordNotFound (Couldn't find Order with 'id'=1553):
  app/controllers/orders_controller.rb:6:in `show'

Running User.find(2454266).orders.find(1553) in the console works.

Also note that it's possible to skip the relation and go directly to the order model, like this

class OrdersController < ApplicationController
  #
  # GET /orders/:id
  #
  def show
    @order = Order.find(params[:id])
  end
end

原文:https://stackoverflow.com/questions/32719359
更新时间:2023-08-16 15:08

最满意答案

如果文件驻留在同一本地服务器上,则不需要将file_get_contents与HTTP URL一起使用。 这样做会产生HTTP请求,并且会增加不必要的延迟。

除非您希望文件由各自的应用程序处理,否则您只需从文件路径加载HTML文件的内容即可

$content = file_get_contents('/path/to/application1/html/file.htm');

如果这些文件包含需要评估的PHP,请使用

include 'path/to/application1/html/file.htm';

代替。 请注意,使用include将在目标文件的开头将PHP置于HTML模式,并在最后再次恢复PHP模式。 因此,您必须使用相应的<?php open标记将任何PHP代码包含在文件中。

如果需要在变量中捕获include调用的输出,请将其包装

ob_start();
include '…'; // this can also take a URL
$content = ob_get_clean();

这将启用输出缓冲

如果您需要处理文件中的HTML,请使用正确的HTML解析器


Well I guess I found a better way of doing it myself so I am posting here as an answer for the sake of others who are looking for a similar solution.

Write a class which will read between the markers and get that bit.

<?Php
class className {
    private $_content;
    private $_Top;
    private $_Bottom;

    public function __construct(
        $url, $markerStartTop = null, 
        $markerEndTop = null, 
        $markerStartBottom = null, 
        $markerEndBottom = null){

            $this->_content = file_get_contents($url);
            $this->_renderTop($markerStartTop, $markerEndTop);
            $this->_renderBottom($markerStartBottom, $markerEndBottom);
        }

    public function renderTop($markerStart = null, $markerEnd = null){
        return $this->_Top;
        }

    private function _renderTop($markerStart = null, $markerEnd = null){
        $markerStart = (is_null($markerStart)) ? '<!– Start Top Block –>' : (string)$markerStart;
        $markerEnd = (is_null($markerEnd)) ? '<!– End Top Block –>' : (string)$markerEnd;

        $TopStart = stripos($this->_content, $markerStart);
        $TopEnd = stripos($this->_content, $markerEnd);

        $this->_Top = substr($this->_content, $TopStart, $TopEnd-$TopStart);
        }

    public function renderBottom(){
        return $this->_Bottom;
        }

    private function _renderBottom($markerStart = null, $markerEnd = null){ 
        $markerStart = (is_null($markerStart)) ? '<!– Start Bottom Block –>' : (string)$markerStart;
        $markerEnd = (is_null($markerEnd)) ? '<!– End Bottom Block –>' : (string)$markerEnd;

        $BottomStart = stripos($this->_content, $markerStart);
        $BottomEnd = stripos($this->_content, $markerEnd);

        $this->_Bottom = substr($this->_content, $BottomStart, $BottomEnd-$BottomStart);
        }
}
?>

Call it:

<?php
    $parts = new className('url/to/the/application');
    echo $parts->renderTop();
?>

This method is fetching exactly the fragment of the content which you marked between the desired markers.

相关问答

更多

相关文章

更多

最新问答

更多
  • python的访问器方法有哪些
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。
  • 响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
  • 在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
  • NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
  • 元素上的盒子阴影行为(box-shadow behaviour on elements)
  • Laravel检查是否存在记录(Laravel Checking If a Record Exists)
  • 设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
  • 想学Linux 运维 深圳有哪个培训机构好一点
  • 为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)
  • 在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)
  • AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)
  • 错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)
  • 等待EC2实例重启(Wait for an EC2 instance to reboot)
  • 如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)
  • 使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)
  • 江民杀毒软件的KSysnon.sys模块是什么东西?
  • 处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)
  • PerSession与PerCall(PerSession vs. PerCall)
  • C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)
  • 平衡一个精灵(Balancing a sprite)
  • n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的