Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #14423 from yakko/persistence-touches-many

ActiveRecord#touch should accept multiple attributes

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
commit 140ea2251b17ee9851e2511bf0a82a9e62197cd7 2 parents eafec46 + c80ca4c
@carlosantoniodasilva carlosantoniodasilva authored
View
12 activerecord/CHANGELOG.md
@@ -1,3 +1,15 @@
+* `ActiveRecord#touch` should accept many attributes at once. Suggested at #14423.
+
+ Example:
+
+ photo = Photo.last
+ photo.touch(:signed_at, :sealed_at)
+ photo.updated_at # was changed
+ photo.signed_at # was changed
+ photo.sealed_at # was changed
+
+ *James Pinto*
+
* `rake db:structure:dump` only dumps schema information if the schema
migration table exists.
View
5 activerecord/lib/active_record/persistence.rb
@@ -412,6 +412,7 @@ def reload(options = nil)
#
# product.touch # updates updated_at/on
# product.touch(:designed_at) # updates the designed_at attribute and updated_at/on
+ # product.touch(:started_at, :ended_at) # updates started_at, ended_at and updated_at/on attributes
#
# If used along with +belongs_to+ then +touch+ will invoke +touch+ method on associated object.
#
@@ -432,11 +433,11 @@ def reload(options = nil)
# ball = Ball.new
# ball.touch(:updated_at) # => raises ActiveRecordError
#
- def touch(name = nil)
+ def touch(*names)
raise ActiveRecordError, "cannot touch on a new record object" unless persisted?
attributes = timestamp_attributes_for_update_in_model
- attributes << name if name
+ attributes.concat(names)
unless attributes.empty?
current_time = current_time_from_proper_timezone
View
12 activerecord/test/cases/timestamp_test.rb
@@ -89,6 +89,18 @@ def test_touching_an_attribute_updates_it
assert_in_delta Time.now, task.ending, 1
end
+ def test_touching_many_attributes_updates_them
+ task = Task.first
+ previous_starting = task.starting
+ previous_ending = task.ending
+ task.touch(:starting, :ending)
+
+ assert_not_equal previous_starting, task.starting
+ assert_not_equal previous_ending, task.ending
+ assert_in_delta Time.now, task.starting, 1
+ assert_in_delta Time.now, task.ending, 1
+ end
+
def test_touching_a_record_without_timestamps_is_unexceptional
assert_nothing_raised { Car.first.touch }
end
Please sign in to comment.
Something went wrong with that request. Please try again.