File tree Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Original file line number Diff line number Diff line change @@ -119,21 +119,25 @@ def rebuild!
119119 end
120120
121121 def cleanup!
122- ids_to_delete = [ ]
123-
124122 hierarchy_table = hierarchy_class . arel_table
125123
124+ query = hierarchy_class
125+ alias_tables = [ ]
126126 [ :descendant_id , :ancestor_id ] . each do |foreign_key |
127- arel_join = hierarchy_table . join ( arel_table , Arel ::Nodes ::OuterJoin )
128- . on ( arel_table [ primary_key ] . eq ( hierarchy_table [ foreign_key ] ) )
127+ alias_name = foreign_key . to_s . split ( '_' ) . first + "s"
128+ alias_table = Arel ::Table . new ( table_name ) . alias ( alias_name )
129+ alias_tables << alias_table
130+ arel_join = hierarchy_table . join ( alias_table , Arel ::Nodes ::OuterJoin )
131+ . on ( alias_table [ primary_key ] . eq ( hierarchy_table [ foreign_key ] ) )
129132 . join_sources
130133
131- where_condition = { }
132- where_condition [ table_name ] = { }
133- where_condition [ table_name ] [ primary_key ] = nil
134-
135- hierarchy_class . joins ( arel_join ) . where ( where_condition ) . destroy_all
134+ query = query . joins ( arel_join )
136135 end
136+
137+ query . where (
138+ alias_tables . first [ primary_key ] . eq ( nil )
139+ . or ( alias_tables . second [ primary_key ] . eq ( nil ) )
140+ ) . destroy_all
137141 end
138142 end
139143 end
You can’t perform that action at this time.
0 commit comments