提交 00ebbb79 编写于 作者: K Kevin Casey

fix HABTM w/out primary key errors on destruction

上级 f484df79
...@@ -111,7 +111,7 @@ def delete_records(records, method) ...@@ -111,7 +111,7 @@ def delete_records(records, method)
records.each(&:destroy!) records.each(&:destroy!)
update_counter(-records.length) unless inverse_updates_counter_cache? update_counter(-records.length) unless inverse_updates_counter_cache?
else else
if records == :all if records == :all || !reflection.klass.primary_key
scope = self.scope scope = self.scope
else else
scope = self.scope.where(reflection.klass.primary_key => records) scope = self.scope.where(reflection.klass.primary_key => records)
......
...@@ -775,6 +775,16 @@ def test_include_method_in_has_and_belongs_to_many_association_should_return_tru ...@@ -775,6 +775,16 @@ def test_include_method_in_has_and_belongs_to_many_association_should_return_tru
assert project.developers.include?(developer) assert project.developers.include?(developer)
end 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 test "has and belongs to many associations on new records use null relations" do
projects = Developer.new.projects projects = Developer.new.projects
assert_no_queries do assert_no_queries do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册