mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
habtm delete method integrated with ARel.
This commit is contained in:
@@ -34,7 +34,7 @@ module ActiveRecord
|
||||
options[:readonly] = finding_with_ambiguous_select?(options[:select] || @reflection.options[:select])
|
||||
options[:select] ||= (@reflection.options[:select] || '*')
|
||||
end
|
||||
|
||||
|
||||
def count_records
|
||||
load_target.size
|
||||
end
|
||||
@@ -85,9 +85,10 @@ module ActiveRecord
|
||||
if sql = @reflection.options[:delete_sql]
|
||||
records.each { |record| @owner.connection.delete(interpolate_sql(sql, record)) }
|
||||
else
|
||||
ids = quoted_record_ids(records)
|
||||
sql = "DELETE FROM #{@owner.connection.quote_table_name @reflection.options[:join_table]} WHERE #{@reflection.primary_key_name} = #{owner_quoted_id} AND #{@reflection.association_foreign_key} IN (#{ids})"
|
||||
@owner.connection.delete(sql)
|
||||
relation = arel_table(@reflection.options[:join_table])
|
||||
relation.conditions(relation[@reflection.primary_key_name].eq(@owner.id).
|
||||
and(Arel::In.new(relation[@reflection.association_foreign_key], records.map(&:id)))
|
||||
).delete
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user