diff options
author | Jon Leighton <j@jonathanleighton.com> | 2010-12-17 10:20:18 +0000 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-12-20 13:56:04 -0800 |
commit | 57b90098930a52fa160f9b1aed2dcea0f557d670 (patch) | |
tree | a2d8dbabd10cdde4a4e2337f538263437bf36e83 /activerecord/lib/active_record/associations | |
parent | b8153fd5a18441567f787a33ca882acb3bb5088a (diff) | |
download | rails-57b90098930a52fa160f9b1aed2dcea0f557d670.tar.gz rails-57b90098930a52fa160f9b1aed2dcea0f557d670.tar.bz2 rails-57b90098930a52fa160f9b1aed2dcea0f557d670.zip |
Refactor delete_records in HasManyAssociation
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_association.rb | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index cfbb49b6a3..bb9dfd4f70 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -62,15 +62,16 @@ module ActiveRecord def delete_records(records) case @reflection.options[:dependent] when :destroy - records.each { |r| r.destroy } + records.each(&:destroy) when :delete_all - @reflection.klass.delete(records.map { |record| record.id }) + @reflection.klass.delete(records.map(&:id)) else - relation = Arel::Table.new(@reflection.table_name) - stmt = relation.where(relation[@reflection.primary_key_name].eq(@owner.id). - and(relation[@reflection.klass.primary_key].in(records.map { |r| r.id })) - ).compile_update(relation[@reflection.primary_key_name] => nil) - @owner.connection.update stmt.to_sql + updates = { @reflection.primary_key_name => nil } + conditions = { @reflection.association_primary_key => records.map(&:id) } + + with_scope(@scope) do + @reflection.klass.update_all(updates, conditions) + end @owner.class.update_counters(@owner.id, cached_counter_attribute_name => -records.size) if has_cached_counter? end |