mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge PR #19759
Fix for has_and_belongs_to_many & has_many_through associations
This commit is contained in:
commit
a0a37d9fd3
3 changed files with 24 additions and 1 deletions
|
@ -1,3 +1,10 @@
|
|||
* Improved partial writes with HABTM and has many through associations
|
||||
to fire database query only if relation has been changed.
|
||||
|
||||
Fixes #19663.
|
||||
|
||||
*Mehmet Emin İNAÇ*
|
||||
|
||||
* Deprecate passing arguments and block at the same time to
|
||||
`ActiveRecord::QueryMethods#select`.
|
||||
|
||||
|
|
|
@ -86,7 +86,10 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def save_through_record(record)
|
||||
build_through_record(record).save!
|
||||
association = build_through_record(record)
|
||||
if association.changed?
|
||||
association.save!
|
||||
end
|
||||
ensure
|
||||
@through_records.delete(record.object_id)
|
||||
end
|
||||
|
|
|
@ -1012,4 +1012,17 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|||
user = User.create!
|
||||
assert_nothing_raised { user.jobs_pool.clear }
|
||||
end
|
||||
|
||||
def test_has_and_belongs_to_many_while_partial_writes_false
|
||||
begin
|
||||
original_partial_writes = ActiveRecord::Base.partial_writes
|
||||
ActiveRecord::Base.partial_writes = false
|
||||
developer = Developer.new(name: "Mehmet Emin İNAÇ")
|
||||
developer.projects << Project.new(name: "Bounty")
|
||||
|
||||
assert developer.save
|
||||
ensure
|
||||
ActiveRecord::Base.partial_writes = original_partial_writes
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue