From ee07a2169089838bb1c66206c0b5188cfb170632 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Tue, 24 Aug 2010 09:56:26 -0400 Subject: [PATCH] @user.touch should not fail if User does not have updated_at/updated_on column. [#5439 state:resolved] --- activerecord/lib/active_record/persistence.rb | 20 +++++++++++--------- activerecord/test/cases/timestamp_test.rb | 8 +++++++- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 0188972..c5c40d1 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -214,18 +214,20 @@ module ActiveRecord # product.touch(:designed_at) # updates the designed_at attribute and updated_at/on def touch(name = nil) attributes = timestamp_attributes_for_update_in_model - attributes << name if name + unless attributes.blank? + attributes << name if name - current_time = current_time_from_proper_timezone - changes = {} + current_time = current_time_from_proper_timezone + changes = {} - attributes.each do |column| - changes[column.to_s] = write_attribute(column.to_s, current_time) - end + attributes.each do |column| + changes[column.to_s] = write_attribute(column.to_s, current_time) + end - @changed_attributes.except!(*changes.keys) - primary_key = self.class.primary_key - self.class.update_all(changes, { primary_key => self[primary_key] }) == 1 + @changed_attributes.except!(*changes.keys) + primary_key = self.class.primary_key + self.class.update_all(changes, { primary_key => self[primary_key] }) == 1 + end end private diff --git a/activerecord/test/cases/timestamp_test.rb b/activerecord/test/cases/timestamp_test.rb index fce27d8..f294650 100644 --- a/activerecord/test/cases/timestamp_test.rb +++ b/activerecord/test/cases/timestamp_test.rb @@ -3,13 +3,15 @@ require 'models/developer' require 'models/owner' require 'models/pet' require 'models/toy' +require 'models/car' class TimestampTest < ActiveRecord::TestCase - fixtures :developers, :owners, :pets, :toys + fixtures :developers, :owners, :pets, :toys, :cars def setup @developer = Developer.first @previously_updated_at = @developer.updated_at + @car = Car.first end def test_saving_a_changed_record_updates_its_timestamp @@ -48,6 +50,10 @@ class TimestampTest < ActiveRecord::TestCase assert_not_equal @previously_updated_at, @developer.updated_at end + def test_touch_a_record_without_timestamps + assert_nothing_raised { @car.touch } + end + def test_saving_a_record_with_a_belongs_to_that_specifies_touching_the_parent_should_update_the_parent_updated_at pet = Pet.first owner = pet.owner -- 1.7.2