aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/timestamp.rb2
-rw-r--r--activerecord/test/cases/timestamp_test.rb22
3 files changed, 29 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index f1f9cf1ffd..460eeb5dfd 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+* Do not try to write timestamps when a table has no timestamps columns.
+
+ Fixes #8813.
+
+ *Sergey Potapov*
+
* Properly detect if a connection is still active before using it
in multi-threaded environments.
diff --git a/activerecord/lib/active_record/timestamp.rb b/activerecord/lib/active_record/timestamp.rb
index 7178bed560..271f58cbe0 100644
--- a/activerecord/lib/active_record/timestamp.rb
+++ b/activerecord/lib/active_record/timestamp.rb
@@ -48,7 +48,7 @@ module ActiveRecord
current_time = current_time_from_proper_timezone
all_timestamp_attributes.each do |column|
- if respond_to?(column) && respond_to?("#{column}=") && self.send(column).nil?
+ if attributes.has_key?(column.to_s) && self.send(column).nil?
write_attribute(column.to_s, current_time)
end
end
diff --git a/activerecord/test/cases/timestamp_test.rb b/activerecord/test/cases/timestamp_test.rb
index 717e0e1866..8195e8a08a 100644
--- a/activerecord/test/cases/timestamp_test.rb
+++ b/activerecord/test/cases/timestamp_test.rb
@@ -9,11 +9,26 @@ require 'models/task'
class TimestampTest < ActiveRecord::TestCase
fixtures :developers, :owners, :pets, :toys, :cars, :tasks
+ # Model with timestamp attribute accessors, but without timestamps columns.
+ class TimestampAttributePost < ActiveRecord::Base
+ attr_accessor :created_at, :updated_at
+ end
+
+
def setup
@developer = Developer.first
@owner = Owner.first
@developer.update_columns(updated_at: Time.now.prev_month)
@previously_updated_at = @developer.updated_at
+
+ @connection = ActiveRecord::Base.connection
+ @connection.create_table 'timestamp_attribute_posts', :force => true do |t|
+ # Must have no timestamps
+ end
+ end
+
+ def teardown
+ @connection.execute 'drop table if exists timestamp_attribute_posts'
end
def test_saving_a_changed_record_updates_its_timestamp
@@ -379,4 +394,11 @@ class TimestampTest < ActiveRecord::TestCase
toy = Toy.first
assert_equal [:created_at, :updated_at], toy.send(:all_timestamp_attributes_in_model)
end
+
+ def test_do_not_write_timestamps_on_save_if_they_are_not_attributes
+ post = TimestampAttributePost.new
+ assert_nothing_raised ActiveModel::MissingAttributeError do
+ post.run_callbacks :save
+ end
+ end
end