activeModel :: dirty记录更改,即使没有任何更改

时间:2014-03-11 19:47:07

标签: ruby-on-rails ruby activerecord activemodel active-model-serializers

我正在使用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认为每次都有变化,我该如何解决?

1 个答案:

答案 0 :(得分:0)

至少timeframeupdated_at已更改。请注意,时间对象只保存了几秒钟,而inspect方法不会显示毫秒数。

但没有人能够告诉你为什么会在没有提供代码的情况下发生这种情况。