1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

fix HABTM w/out primary key errors on destruction

This commit is contained in:
Kevin Casey 2014-02-02 11:47:06 -08:00
parent f484df79f2
commit 00ebbb7945
2 changed files with 11 additions and 1 deletions

View file

@ -111,7 +111,7 @@ module ActiveRecord
records.each(&:destroy!)
update_counter(-records.length) unless inverse_updates_counter_cache?
else
if records == :all
if records == :all || !reflection.klass.primary_key
scope = self.scope
else
scope = self.scope.where(reflection.klass.primary_key => records)

View file

@ -775,6 +775,16 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
assert project.developers.include?(developer)
end
def test_destruction_does_not_error_without_primary_key
redbeard = pirates(:redbeard)
george = parrots(:george)
redbeard.parrots << george
assert_equal 2, george.pirates.count
Pirate.includes(:parrots).where(parrot: redbeard.parrot).find(redbeard.id).destroy
assert_equal 1, george.pirates.count
assert_equal [], Pirate.where(id: redbeard.id)
end
test "has and belongs to many associations on new records use null relations" do
projects = Developer.new.projects
assert_no_queries do