关系随机引发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
最满意答案
如果文件驻留在同一本地服务器上,则不需要将
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.
相关问答
更多-
在这种情况下,您可以在xpath查询中使用OR 。 只需将这些标签与其级联,即可得到那些唯一需要的标签。 $url = "http://www.example.com/russian/international/2015/02/150218_ukraine_debaltseve_fighting"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); $html = curl_exec($curl); curl ...
-
从div元素中提取内容(Extract content from div elements)[2022-08-29]
该代码应该由javascript本身解析,转换特殊字符。 它是一个json对象,所以你可以解码它并检索你想要的东西。 请注意,对于您想要的rappresentation,我使用了\n和\n 。 既然您在文档上打印,应该足够了。 编辑 我忘了指出你可以用数据存储属性直接检索所有div。 你应该考虑的精明是在使用之前检查json属性是否存在。 即`let time_stamp =(data.timestamp!= undefined)? data.timestamp:null; let csv = 'times ... -
你需要使用jQuery“load”方法来加载它(像这样): $('#result').load('exampledomain.com #targetdiv'); 请注意,域名和#target之间有一个空格 You need to make use of jQuery "load" method to load it (like this): $('#result').load('exampledomain.com #targetdiv'); Please note there is a space ...
-
你可能正在尝试很长的路要走。 您可以使用视图模块。 它允许创建查询数据库的自定义列表,但也可以覆盖现有的列表,例如分类术语页面列表的情况。 一旦安装了模块(如果还没有),特别是Views UI模块,请转到/admin/structure/views并滚动到底部,其中禁用视图(灰色行)。 您将找到一个名为Taxonomy的术语 ,描述为“模拟Drupal核心处理分类/术语的视图”。 单击右侧的“ 启用 ”,然后转到“启用”链接所在的同一位置,单击要展开的箭头,然后选择“ 编辑” 。 进入视图编辑页面后,您可以 ...
-
如何将sharepoint站点从一台服务器完全转移到另一台服务器?(How can I completely transfer sharepoint site from one server to other?)[2022-04-12]
stsadm -o backup -url http:// oldportal -filename C:\ oldportal.dat 将.dat带到新服务器,使用空白网站集创建新的WebApplication stsadm -o restore -url http:// newportal -filename C:\ oldportal.dat -overwrite 自定义功能和解决方案不会被转移,但您提到的项目(工作流程,人员,组)将全部转移。 stsadm -o backup -url http:// ... -
这是一种非常常见的做法。 有时几个应用程序共享数据库。 有时,数据库位于其自己的服务器上,因此可以针对数据库优化服务器,并且数据库可以使用所有服务器资源,而不是与Web服务器共享它们。 无论调用应用程序是否在同一台计算机上,都可以通过在数据库中设置权限来处理安全性。 通常,应用程序将使用自己的身份和凭据进行操作。 他们对数据库的权限非常有限。 This is a very common practice. Sometimes several applications are sharing the data ...
-
正如我所说它可能是一个gzipped输出,使用此功能并通过它传递“乱码”。 如果它不是问题让我知道生病了删除这个答案 $site = file_get_contents('http://example.com'); echo gzdecoder($site); function gzdecoder($d){ $f=ord(substr($d,3,1)); $h=10;$e=0; if($f&4){ $e=unpack('v',substr($d,10,2)); ...
-
使用nginx,我如何在从另一台服务器返回的页面上运行SSI?(With nginx, how do I run SSI on a page returned from another server?)[2022-09-10]
确保server1没有返回压缩内容。 如果它被返回gzip,nginx将不会解压缩以将ssi规则应用于它。 您可以通过清除Accept-Encoding标头确保以明文形式返回响应。 location /hello-world.html { ssi on; proxy_set_header Accept-Encoding ""; proxy_pass http://tom.office.bla.co.uk:8080/hello-world/; } Make sure that se ... -
如果文件驻留在同一本地服务器上,则不需要将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' ...
-
您可能没有从旧网站复制.htaccess文件,您需要将其从旧的Joomla网站复制到新网站。 这通常发生是因为.htaccess文件被认为是隐藏文件,因此不会自动复制。 You probably haven't copied the .htaccess file from the old website, you will need to copy it from the old Joomla website to the new one. This typically happens because th ...