diff options
author | Guillermo Iguaran <guilleiguaran@gmail.com> | 2013-02-24 21:30:54 -0800 |
---|---|---|
committer | Guillermo Iguaran <guilleiguaran@gmail.com> | 2013-02-24 21:30:54 -0800 |
commit | 3285c76557f1c36aba4de17d3440e16220cd87d8 (patch) | |
tree | 21e150731afe39e38803ae2339febed32f4f1532 | |
parent | 3762ee0baa2285bdc4a76d4d492fbe34a10cdc3a (diff) | |
parent | e52ff809606323f7370cfaf6e63b083f9b108d6c (diff) | |
download | rails-3285c76557f1c36aba4de17d3440e16220cd87d8.tar.gz rails-3285c76557f1c36aba4de17d3440e16220cd87d8.tar.bz2 rails-3285c76557f1c36aba4de17d3440e16220cd87d8.zip |
Merge pull request #9320 from wangjohn/touch_handling_on_new_instances
Raising an ActiveRecordError when one tries to use touch on a new record object
-rw-r--r-- | activerecord/CHANGELOG.md | 11 | ||||
-rw-r--r-- | activerecord/lib/active_record/persistence.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 7 |
3 files changed, 20 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index b3301f70dc..c7085a0eaa 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,16 @@ ## Rails 4.0.0 (unreleased) ## +* Fixing issue #8345. Now throwing an error when one attempts to touch a + new object that has not yet been persisted. For instance: + + ball = Ball.new + ball.touch :updated_at # => raises error + + It is not until the ball object has been persisted that it can be touched. + This follows the behavior of update_column. + + *John Wang* + * Preloading ordered `has_many :through` associations no longer applies invalid ordering to the `:through` association. Fixes #8663. diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 066f93635a..26c11b50a2 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -368,6 +368,8 @@ module ActiveRecord # # triggers @brake.car.touch and @brake.car.corporation.touch # @brake.touch def touch(name = nil) + raise ActiveRecordError, "can not touch on a new record object" unless persisted? + attributes = timestamp_attributes_for_update_in_model attributes << name if name diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index fbc66540d6..af1845c937 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1447,6 +1447,13 @@ class BasicsTest < ActiveRecord::TestCase assert_match(/\/#{dev.id}$/, dev.cache_key) end + def test_touch_should_raise_error_on_a_new_object + company = Company.new(:rating => 1, :name => "37signals", :firm_name => "37signals") + assert_raises(ActiveRecord::ActiveRecordError) do + company.touch :updated_at + end + end + def test_cache_key_format_is_precise_enough dev = Developer.first key = dev.cache_key |