YAML,delayed_job:心理vs赛克.如何使pysch读取ruby对象的attr_accessors [英] YAML, delayed_job : Psych vs Syck. How to make pysch read attr_accessors for a ruby object

查看:107
本文介绍了YAML,delayed_job:心理vs赛克.如何使pysch读取ruby对象的attr_accessors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用ruby 1.9.3的delay_job(3.0.3)时遇到问题.以前我们使用的是ruby 1.8.7,它与yaml syck解析器一起提供,该解析器读取为ruby对象设置的所有属性(包括attr_accessors),但随着升级到1.9.3,yaml解析器被切换为psych(重新文字),并且除了数据库中保留的那些属性外,它不考虑任何其他属性.我们又如何才能使attr_accessors也被考虑在内.我试图通过以下方式切换到syck:

I'm having problems using delayed_job (3.0.3) with ruby 1.9.3. Previously we were using ruby 1.8.7 which comes with yaml syck parser which read all the attributes that are set for a ruby object (including attr_accessors) but with the upgrade to 1.9.3 the yaml parser was switched to psych (which was re-written) and it doesn't take into account any attributes except those persisted in the database. How can we make psych to take the attr_accessors into account as well. I tried to switch to syck thru:

YAML::ENGINE.yamler = 'syck'

但仍然无法正常工作.

有人可以解决此问题吗?

Does anyone have a work around for this issue?

推荐答案

上述方法无效,但我们需要做的就是重写ActiveRecord :: Base的encode_with和init_with方法以包括属性访问器.更准确地说,我们需要使用att_accessors设置编码器哈希,这要注意实例变量的持久性.

The above hack doesn't work but all we need is to override the encode_with and init_with methods of ActiveRecord::Base to include attribute accessors. More precisely we need to set the coder hash with the att_accessors and that takes care of instance variable persistance.

有趣的是: https://gist.github.com/3011499

这篇关于YAML,delayed_job:心理vs赛克.如何使pysch读取ruby对象的attr_accessors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆