writer and custom reader not working as advertised

Ruby Version: 1.8
Rails Version: 2.3

I have a class in which I have written 'attr_writer :content' and I have a custom reader method 'content'. Doing it this way, content is NULL. I've checked out the values and they look like this:

--- &id001 !ruby/object:Email 
attributes: 
  created_at: 
  expired_at: 
  updated_at: 
  subject: Test Eblast
  content: 
  inline_attachments: 
  attachments: 
attributes_cache: 
  expired_at: 
changed_attributes: 
  subject: 
content: This is the body
errors: !ruby/object:ActiveRecord::Errors 
  base: *id001
  errors: !omap []

new_record: true

Notice the indentation of "content:" on the 2nd instance. It isn't being written as an attribute, but something else...while the actual attribute isn't being updated. When I take out 'attr_writer' and write my own getter/setter methods explicitly, everything works. Why is that?


attr_writer为实例变量创建getter和setter方法,而不是ActiveRecord属性。

链接地址: http://www.djcxy.com/p/25728.html

上一篇: 介绍ruby / rails对象?

下一篇: 作家和自定义阅读器不像广告一样工作