Yaml::load_file 在开发和生产之间表现不同 (Rails)

发布于 2024-08-25 20:08:47 字数 1020 浏览 5 评论 0原文

我对这个问题的本质完全感到困惑。

我们将应用程序中的数据导出到“清理过的”YAML 文件中(删除 ID、created_at 等)。然后我们(将)允许用户将这些文件导入回应用程序中——正是导入完全让我烦恼。

在开发中,YAML::load_file(params[:uploaded_data].local_path) 返回 YAML::Objects 的数组(并且文件加载的不同方式中的哪一种并不重要) ):

[#<YAML::Object:0x3c63984 @class="Event", @ivars={"attributes"=>{"exception_count"=>"0", "title"=>"Start", "amount"=>"70.00", "colour"=>nil, "repeat_type_id"=>"0", "repeat_interval"=>"1"}}>, etc etc]

这非常好,因为属性还包括您看到的 exception_count 的(关联模型)异常。

然而,在生产中(rails 2.3.2,运行 REE 1.8.7 和 1.8.6 进行测试,在两个不同的生产环境中进行测试,并在本地运行生产),它会返回 YAML 中的对象数组 - 在本例中为事件:

[#<Event title: "Start", amount: #<BigDecimal:3af2640,'0.7E2',4(8)>, repeat_type_id: 0, colour: nil,  repeat_interval: 1, exception_count: 0>, etc etc]

现在,如果它还包含关联的模型异常,那将会令人困惑 - 但事实并非如此。

任何人都可以解释一下为什么 Yaml 解析器在生产和开发之间的行为如此不同吗?

我使用的是 Rails 2.3.2,运行 REE 1.8.7;不过我也测试过运行 Ruby 1.8.6,结果完全相同。

感谢您的帮助!

I am completely stumped at the nature of this problem.

We export data from our application into a 'cleaned' YAML file (stripping out IDs, created_at etc). Then we (will) allow users to import these files back into the application - it is the import that is completely bugging me out.

In development, YAML::load_file(params[:uploaded_data].local_path) returns an array of YAML::Objects's (and it doesn't matter which of the number of different ways the file could be loaded):

[#<YAML::Object:0x3c63984 @class="Event", @ivars={"attributes"=>{"exception_count"=>"0", "title"=>"Start", "amount"=>"70.00", "colour"=>nil, "repeat_type_id"=>"0", "repeat_interval"=>"1"}}>, etc etc]

Which is very nice, as the attributes also include the (associated model) exceptions that you see an exception_count for.

However on production (rails 2.3.2, running REE 1.8.7 and 1.8.6 for testing, tested on two different production env's, and running production locally) it returns an array of the Objects within the YAML - in this case, Event:

[#<Event title: "Start", amount: #<BigDecimal:3af2640,'0.7E2',4(8)>, repeat_type_id: 0, colour: nil,  repeat_interval: 1, exception_count: 0>, etc etc]

Now this would be just perplexing if it also included the associated model Exception with it - however it doesn't.

Can anyone at all shed some light on why the Yaml parser would behave so differently between production and development?

I'm on rails 2.3.2, running REE 1.8.7; however I've also tested running Ruby 1.8.6 with exactly the same results.

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铜锣湾横着走 2024-09-01 20:08:47

我认为这可能是由于在生产和开发中 cache_classes 设置为 truefalse 造成的。 YAML::load_file 方法将尝试查找与这些对象相关的类,并且由于它们尚未加载,因此只会将它们创建为 YAML 对象。

首先尝试通过调用 Event 来“加载”Event 类,然后查看是否可以修复该问题。

I think this may be caused by cache_classes being set to true in production and in development false. The YAML::load_file method would try to find classes pertaining to these objects and because they haven't been loaded yet will just create them as YAML objects.

Try "loading" the Event class first simply by calling Event and see if that fixes it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文