我有一个看起来像这样的模型:
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"]
任何人都可以解释这种差异或提出解决方法吗?