提交 747e0744 编写于 作者: N Neeraj Singh 提交者: José Valim

@user.touch should not fail if User does not have updated_at/updated_on column.

[#5439 state:resolved]
Signed-off-by: NJosé Valim <jose.valim@gmail.com>
上级 11f6179f
......@@ -214,18 +214,20 @@ def reload(options = nil)
# 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
......
......@@ -3,13 +3,15 @@
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
......@@ -58,6 +60,10 @@ def test_touching_a_different_attribute
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册