ruby on rails - activeModel::脏记录即使什么都没改变也会改变



我正在使用ActiveModel::Dirty库。我有以下代码:

def tasks_changed?
   @changed = false
   self.tasks.each do |task|
     if task.previous_changes.any?
       @changed = true
       puts 'task changed so no update'
       puts 'this task changed' + task.inspect.to_s
       puts 'here are the changes' + task.previous_changes.to_s
     end
   end
   return @changed
end

如果用户更改表单中的内容,该方法将更改控制器的行为。问题是其中一个字段是日期时间,由于某种原因,previous_changes认为日期时间每次都在变化,而不管它是否被更改。

控制台甚至告诉我没有变化。下面是上面的puts语句在控制台中产生的结果:

task changed so no update
this task changed#<Task id: 19, title: "task 1", content: "task 2 content", created_at: "2014-03-11 17:33:26", updated_at: "2014-03-11 20:00:01", schedule_id: 1, amount: nil, time_frame: "2014-03-27 20:00:00", state: "incomplete", denied: 0, order_number: 0, finished_description: nil>
here are the changes{"time_frame"=>[Thu, 27 Mar 2014 16:00:00 EDT -04:00, Thu, 27 Mar 2014 16:00:00 EDT -04:00], "updated_at"=>[Tue, 11 Mar 2014 15:57:44 EDT -04:00, Tue, 11 Mar 2014 16:00:01 EDT -04:00]}
task changed so no update
this task changed#<Task id: 21, title: "task 2", content: "task 2 content", created_at: "2014-03-11 17:42:18", updated_at: "2014-03-11 20:00:01", schedule_id: 1, amount: nil, time_frame: "2014-03-29 20:00:00", state: "incomplete", denied: 0, order_number: 1, finished_description: nil>
here are the changes{"time_frame"=>[Sat, 29 Mar 2014 16:00:00 EDT -04:00, Sat, 29 Mar 2014 16:00:00 EDT -04:00], "updated_at"=>[Tue, 11 Mar 2014 15:57:44 EDT -04:00, Tue, 11 Mar 2014 16:00:01 EDT -04:00]}
task changed so no update
this task changed#<Task id: 22, title: "task 3 ", content: "change", created_at: "2014-03-11 18:43:23", updated_at: "2014-03-11 20:00:01", schedule_id: 1, amount: nil, time_frame: "2014-03-31 20:00:00", state: "incomplete", denied: 0, order_number: 2, finished_description: nil>
here are the changes{"time_frame"=>[Mon, 31 Mar 2014 16:00:00 EDT -04:00, Mon, 31 Mar 2014 16:00:00 EDT -04:00], "updated_at"=>[Tue, 11 Mar 2014 15:57:44 EDT -04:00, Tue, 11 Mar 2014 16:00:01 EDT -04:00]}

我的基本问题是为什么previous_changes认为每次都有变化,我如何修复它?

至少timeframeupdated_at发生了变化。请注意,a time对象保存的不仅仅是秒,而且inspect方法不会显示毫秒。

但是没有人能够告诉你为什么会发生这种情况,除非提供这样做的代码

相关内容

  • 没有找到相关文章

最新更新