Permalink
Browse files

has_many nullify associations keys using ARel.

  • Loading branch information...
1 parent a717877 commit a09215ee968250963256a39936a4136aeb1b65f6 @miloops miloops committed Aug 18, 2009
Showing with 9 additions and 9 deletions.
  1. +9 −9 activerecord/lib/active_record/associations/has_many_association.rb
@@ -40,11 +40,11 @@ def count_records
# 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 @@ def delete_records(records)
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 @@ def construct_sql
@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

0 comments on commit a09215e

Please sign in to comment.