提交 1ecc3e83 编写于 作者: P Paul Nikitochkin

#11288: Removed duplicated touching

if belongs to model with touch option on touch

Closes #11288
上级 5ade0ddf
* Remove extra select and update queries on save/touch/destroy ActiveRecord model
with belongs to reflection with option `touch: true`.
Fixes: #11288
*Paul Nikitochkin*
* Remove deprecated `ActiveRecord::Base.disable_implicit_join_references=`. * Remove deprecated `ActiveRecord::Base.disable_implicit_join_references=`.
*Arun Agrawal* *Arun Agrawal*
......
...@@ -92,7 +92,7 @@ def add_counter_cache_callbacks(reflection) ...@@ -92,7 +92,7 @@ def add_counter_cache_callbacks(reflection)
end end
def self.touch_record(o, foreign_key, name, touch) # :nodoc: def self.touch_record(o, foreign_key, name, touch) # :nodoc:
old_foreign_id = o.attribute_was(foreign_key) old_foreign_id = o.changed_attributes[foreign_key]
if old_foreign_id if old_foreign_id
klass = o.association(name).klass klass = o.association(name).klass
......
require "cases/helper" require 'cases/helper'
require 'models/developer' require 'models/developer'
require 'models/project' require 'models/project'
require 'models/company' require 'models/company'
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
require 'models/member' require 'models/member'
require 'models/essay' require 'models/essay'
require 'models/toy' require 'models/toy'
require 'models/invoice'
require 'models/line_item'
class BelongsToAssociationsTest < ActiveRecord::TestCase class BelongsToAssociationsTest < ActiveRecord::TestCase
fixtures :accounts, :companies, :developers, :projects, :topics, fixtures :accounts, :companies, :developers, :projects, :topics,
...@@ -324,6 +326,45 @@ def test_belongs_to_counter_after_save ...@@ -324,6 +326,45 @@ def test_belongs_to_counter_after_save
assert_equal 1, Topic.find(topic.id)[:replies_count] assert_equal 1, Topic.find(topic.id)[:replies_count]
end end
def test_belongs_to_with_touch_option_on_touch
line_item = LineItem.create!
Invoice.create!(line_items: [line_item])
assert_queries(1) { line_item.touch }
end
def test_belongs_to_with_touch_option_on_touch_and_removed_parent
line_item = LineItem.create!
Invoice.create!(line_items: [line_item])
line_item.invoice = nil
assert_queries(2) { line_item.touch }
end
def test_belongs_to_with_touch_option_on_update
line_item = LineItem.create!
Invoice.create!(line_items: [line_item])
assert_queries(2) { line_item.update amount: 10 }
end
def test_belongs_to_with_touch_option_on_destroy
line_item = LineItem.create!
Invoice.create!(line_items: [line_item])
assert_queries(2) { line_item.destroy }
end
def test_belongs_to_with_touch_option_on_touch_and_reassigned_parent
line_item = LineItem.create!
Invoice.create!(line_items: [line_item])
line_item.invoice = Invoice.create!
assert_queries(3) { line_item.touch }
end
def test_belongs_to_counter_after_update def test_belongs_to_counter_after_update
topic = Topic.create!(title: "37s") topic = Topic.create!(title: "37s")
topic.replies.create!(title: "re: 37s", content: "rails") topic.replies.create!(title: "re: 37s", content: "rails")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册