0

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

ユーザーがフォームで何かを変更すると、このメソッドはコントローラーの動作を変更します。問題は、フィールドの 1 つが日時であり、なんらかの理由で、日時が変更されたかどうかに関係なく、日時が毎回変更されると見なしていることです。

コンソールは、変更がないことを教えてくれます。上記の 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 が毎回変更があると考える理由と、それを修正するにはどうすればよいかということです。

4

1 に答える 1