From a09215ee968250963256a39936a4136aeb1b65f6 Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Tue, 18 Aug 2009 20:02:14 -0300 Subject: has_many nullify associations keys using ARel. --- .../active_record/associations/has_many_association.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 73d3c23cd3..29ba84ee37 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -40,11 +40,11 @@ module ActiveRecord # we are certain the current target is an empty array. This is a # documented side-effect of the method that may avoid an extra SELECT. @target ||= [] and loaded if count == 0 - + if @reflection.options[:limit] count = [ @reflection.options[:limit], count ].min end - + return count end @@ -69,11 +69,11 @@ module ActiveRecord when :delete_all @reflection.klass.delete(records.map { |record| record.id }) else - ids = quoted_record_ids(records) - @reflection.klass.update_all( - "#{@reflection.primary_key_name} = NULL", - "#{@reflection.primary_key_name} = #{owner_quoted_id} AND #{@reflection.klass.primary_key} IN (#{ids})" - ) + relation = arel_table(@reflection.table_name) + relation.conditions(relation[@reflection.primary_key_name].eq(@owner.id). + and(Arel::In.new(relation[@reflection.klass.primary_key], records.map(&:id))) + ).update(relation[@reflection.primary_key_name] => nil) + @owner.class.update_counters(@owner.id, cached_counter_attribute_name => -records.size) if has_cached_counter? end end @@ -88,11 +88,11 @@ module ActiveRecord @finder_sql = interpolate_sql(@reflection.options[:finder_sql]) when @reflection.options[:as] - @finder_sql = + @finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_id = #{owner_quoted_id} AND " + "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}" @finder_sql << " AND (#{conditions})" if conditions - + else @finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} = #{owner_quoted_id}" @finder_sql << " AND (#{conditions})" if conditions -- cgit v1.2.3