简体   繁体   English

活动资​​源查找在生产中不起作用

[英]Active resource find doesn't work in production

As the title says, I have a simple ActiveResource in my application that is supposed to get data from an api.正如标题所说,我的应用程序中有一个简单的 ActiveResource,它应该从 api 获取数据。 The collection works perfectly both locally and on the production server.该集合在本地和生产服务器上都能完美运行。 However, .find doesn't work in production, i get a weird error:但是, .find 在生产中不起作用,我收到一个奇怪的错误:

MyResource.find(1, params: { website_id: 2 })
ArgumentError: wrong number of arguments (given 2, expected 0..1)

The same query works if I run it from local console connected to the api.如果我从连接到 api 的本地控制台运行它,则相同的查询有效。 API returns a valid response. API 返回有效响应。 I have no idea how else to debug it.我不知道如何调试它。

class MyResource < ActiveResource::Base
  self.site = Rails.configuration.content_url
  self.prefix = "/api/websites/:website_id/"
  self.element_name = "game"
end

I have to say, i have other resources in the application using the same api, find method works for them, only this one has issues.我不得不说,我在应用程序中有其他资源使用相同的 api,find 方法适用于它们,只有这个有问题。 for some reason it makes a call to出于某种原因,它调用

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/core.rb#L330 with 2 arguments, but i can't figure out who makes such call. https://github.com/rails/rails/blob/master/activerecord/lib/active_record/core.rb#L330有 2 个参数,但我不知道是谁打了这样的电话。 And it only happens in production mode, not in dev.它只发生在生产模式下,而不是在开发模式下。

I found the problem, but it's the weirdest thing ever.我发现了问题,但这是有史以来最奇怪的事情。 In my show method in the api controller i was returning an object but i was merging the hash with some extra data.在 api 控制器中的 show 方法中,我正在返回一个对象,但我正在将哈希与一些额外数据合并。 If i opened in the browser, the response was fine, development worked, but for some reason, in production it doesn't want to accept the merge.如果我在浏览器中打开,响应很好,开发工作正常,但由于某种原因,在生产中它不想接受合并。 So problem solved, but still have no idea why it's happening like that.所以问题解决了,但仍然不知道为什么会这样。

I ran into a similar issue in an app that used ActiveRecord for a vendor model and ActiveResource for a delivery model that has a vendor attribute.我在一个应用程序中遇到了类似的问题,该应用程序将 ActiveRecord 用于供应商模型,并将 ActiveResource 用于具有供应商属性的交付模型。 In development, if the vendor model has not yet been loaded, you can fetch a delivery and deserialize it, including the vendor attribute.在开发中,如果供应商模型尚未加载,您可以获取交付并将其反序列化,包括供应商属性。 If, however, the vendor model has been loaded already, attempting to fetch a delivery leads to the error described in this issue.但是,如果已经加载了供应商模型,则尝试获取交付会导致此问题中描述的错误。

How to reproduce in a console:如何在控制台中重现:

Vendor.connection
Delivery.find(1)

Seems that AResource is trying to create a new instance of Vendor.似乎 AResource 正在尝试创建 Vendor 的新实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM