Skip to content

Commit

Permalink
Use use fk_name for _id column
Browse files Browse the repository at this point in the history
  • Loading branch information
carbonin committed Jan 15, 2018
1 parent eddc4c0 commit 3ec1365
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/models/mixins/purging_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3ec1365

Please sign in to comment.