File tree Expand file tree Collapse file tree 3 files changed +20
-16
lines changed
activerecord/lib/active_record/associations Expand file tree Collapse file tree 3 files changed +20
-16
lines changed Original file line number Diff line number Diff line change @@ -194,7 +194,7 @@ def delete_all(dependent = nil)
194
194
options [ :dependent ]
195
195
end
196
196
197
- delete_records ( :all , dependent ) . tap do
197
+ delete_or_nullify_all_records ( dependent ) . tap do
198
198
reset
199
199
loaded!
200
200
end
Original file line number Diff line number Diff line change @@ -105,23 +105,27 @@ def inverse_updates_counter_cache?(reflection = reflection())
105
105
}
106
106
end
107
107
108
+ def delete_count ( method , scope )
109
+ if method == :delete_all
110
+ scope . delete_all
111
+ else
112
+ scope . update_all ( reflection . foreign_key => nil )
113
+ end
114
+ end
115
+
116
+ def delete_or_nullify_all_records ( method )
117
+ count = delete_count ( method , self . scope )
118
+ update_counter ( -count )
119
+ end
120
+
108
121
# Deletes the records according to the <tt>:dependent</tt> option.
109
122
def delete_records ( records , method )
110
123
if method == :destroy
111
124
records . each ( &:destroy! )
112
125
update_counter ( -records . length ) unless inverse_updates_counter_cache?
113
126
else
114
- if records == :all || !reflection . klass . primary_key
115
- scope = self . scope
116
- else
117
- scope = self . scope . where ( reflection . klass . primary_key => records )
118
- end
119
-
120
- if method == :delete_all
121
- update_counter ( -scope . delete_all )
122
- else
123
- update_counter ( -scope . update_all ( reflection . foreign_key => nil ) )
124
- end
127
+ scope = self . scope . where ( reflection . klass . primary_key => records )
128
+ update_counter ( -delete_count ( method , scope ) )
125
129
end
126
130
end
127
131
Original file line number Diff line number Diff line change @@ -130,13 +130,13 @@ def update_through_counter?(method)
130
130
end
131
131
end
132
132
133
+ def delete_or_nullify_all_records ( method )
134
+ delete_records ( load_target , method )
135
+ end
136
+
133
137
def delete_records ( records , method )
134
138
ensure_not_nested
135
139
136
- # This is unoptimised; it will load all the target records
137
- # even when we just want to delete everything.
138
- records = load_target if records == :all
139
-
140
140
scope = through_association . scope
141
141
scope . where! construct_join_attributes ( *records )
142
142
You can’t perform that action at this time.
0 commit comments