mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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
This commit is contained in:
commit
2574212423
3 changed files with 20 additions and 1 deletions
|
@ -1,3 +1,9 @@
|
|||
* 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*
|
||||
|
||||
* `ActiveRecord::ReadOnlyRecord` now has a descriptive message.
|
||||
|
||||
*Franky W.*
|
||||
|
|
|
@ -33,8 +33,14 @@ module ActiveRecord
|
|||
reload
|
||||
end
|
||||
|
||||
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.create(klass, self)
|
||||
else
|
||||
@proxy ||= CollectionProxy.create(klass, self)
|
||||
end
|
||||
end
|
||||
|
||||
# Implements the writer method, e.g. foo.items= for Foo.has_many :items
|
||||
def writer(records)
|
||||
|
|
|
@ -413,6 +413,13 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
|
|||
assert_equal "Summit", Firm.all.merge!(:order => "id").first.clients_using_primary_key.first.name
|
||||
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, "belongs_to failed sanity check on new object"
|
||||
|
|
Loading…
Reference in a new issue