我的模型看起来像这样:
class WorkRequest < ActiveRecord::Base
attr_accessible :upload, :assigned_to_staff
serialize :assigned_to_staff, Array
before_save :set_old_staff
def set_old_staff
@old_staff = self.assigned_to_staff_was
end
def staff_changed?
!self.assigned_to_staff.empty? && self.assigned_to_staff != @old_staff
end
end
我正在尝试使用self.assigned_to_was来跟踪员工分配更改的时间。我注意到序列化字段的行为与常规字段不同。下面的控制台输出显示了不同的行为:upload(文本字符串字段)和序列化:assigned_to_staff:
1.9.2-p320 :002 > wr.upload
=> nil
1.9.2-p320 :003 > wr.upload_was
=> nil
1.9.2-p320 :004 > wr.upload = "Yes"
=> "Yes"
1.9.2-p320 :005 > wr.upload_was
=> nil
与之相比:
1.9.2-p320 :006 > wr.assigned_to_staff
=> []
1.9.2-p320 :007 > wr.assigned_to_staff_was
=> []
1.9.2-p320 :008 > wr.assigned_to_staff << User.last.name
User Load (0.2ms) SELECT `users`.* FROM `users` ORDER BY `users`.`id` DESC LIMIT 1
=> ["last5, first5"]
1.9.2-p320 :009 > wr.assigned_to_staff_was
=> ["last5, first5"]
任何人都可以解释这种差异并建议解决方法吗?
答案 0 :(得分:1)
序列化似乎并未完全实现主机类的所有方法。为getter和setter提供了覆盖,但没有为连接提供覆盖。