提交 76b58788 编写于 作者: Y Yves Senn

Merge pull request #8813 from greyblake/dont_write_timestamps_if_they_are_not_attributes

Write timestamps only if there are timestamps columns

Conflicts:
	activerecord/CHANGELOG.md
* Do not try to write timestamps when a table has no timestamps columns.
Fixes #8813.
*Sergey Potapov*
* `index_exists?` with `:name` option does verify specified columns.
Example:
......
......@@ -47,7 +47,7 @@ def _create_record
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.key?(column.to_s) && self.send(column).nil?
write_attribute(column.to_s, current_time)
end
end
......
require 'cases/helper'
require 'support/ddl_helper'
require 'models/developer'
require 'models/owner'
require 'models/pet'
......@@ -424,3 +425,21 @@ def test_all_timestamp_attributes_in_model
assert_equal [:created_at, :updated_at], toy.send(:all_timestamp_attributes_in_model)
end
end
class TimestampsWithoutTransactionTest < ActiveRecord::TestCase
include DdlHelper
self.use_transactional_fixtures = false
class TimestampAttributePost < ActiveRecord::Base
attr_accessor :created_at, :updated_at
end
def test_do_not_write_timestamps_on_save_if_they_are_not_attributes
with_example_table ActiveRecord::Base.connection, "timestamp_attribute_posts", "id integer primary key" do
post = TimestampAttributePost.new(id: 1)
post.save! # should not try to assign and persist created_at, updated_at
assert_nil post.created_at
assert_nil post.updated_at
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册