diff --git a/app/models/mixins/purging_mixin.rb b/app/models/mixins/purging_mixin.rb index a1af6923249..8f74a13ec84 100644 --- a/app/models/mixins/purging_mixin.rb +++ b/app/models/mixins/purging_mixin.rb @@ -111,12 +111,13 @@ def purge_orphans(fk_name, window) # For now, this only supports polymorphic references # We don't currently have a situation where a table with a regular reference needs to be purged polymorphic_type_column = "#{fk_name}_type" + polymorphic_id_column = connection.quote_column_name("#{fk_name}_id") total = 0 polymorphic_classes(polymorphic_type_column).each do |klass| resource_table = connection.quote_table_name(klass.table_name) - scope = joins("LEFT OUTER JOIN #{resource_table} ON #{table_name}.resource_id = #{resource_table}.id") + scope = joins("LEFT OUTER JOIN #{resource_table} ON #{table_name}.#{polymorphic_id_column} = #{resource_table}.id") .where(resource_table => {:id => nil}) .where("#{table_name}.#{connection.quote_column_name(polymorphic_type_column)} = #{connection.quote(klass.name)}") total += purge_in_batches(scope, window)