提交 0fc8d88d 编写于 作者: R Rafael Mendonça França

Merge pull request #11694 from Empact/association-bind-values-not-updated-on-save

Fix that a collection proxy could be cached before the save of the owner, resulting in an invalid proxy lacking the owner’s id

Conflicts:
	activerecord/CHANGELOG.md

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/associations/collection_association.rb
	activerecord/test/cases/associations/has_many_associations_test.rb
上级 820e294e
* Cache `CollectionAssociation#reader` proxies separately before and after
the owner has been saved so that the proxy is not cached without the
owner's id.
*Ben Woosley*
## Rails 4.0.10 (September 11, 2014) ##
* Fixed a regression where whitespaces were stripped from DISTINCT queries in
......
......@@ -34,7 +34,13 @@ def reader(force_reload = false)
reload
end
@proxy ||= CollectionProxy.new(klass, self)
if owner.new_record?
# Cache the proxy separately before the owner has an id
# or else a post-save proxy will still lack the id
@new_record_proxy ||= CollectionProxy.new(klass, self)
else
@proxy ||= CollectionProxy.new(klass, self)
end
end
# Implements the writer method, e.g. foo.items= for Foo.has_many :items
......
......@@ -411,6 +411,13 @@ def test_counting_using_finder_sql
assert_equal 2, Firm.find(4).clients_using_sql.count
end
def test_update_all_on_association_accessed_before_save
firm = Firm.new(name: 'Firm')
firm.clients << Client.first
firm.save!
assert_equal firm.clients.count, firm.clients.update_all(description: 'Great!')
end
def test_belongs_to_sanity
c = Client.new
assert_nil c.firm
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册