File tree 1 file changed +13
-9
lines changed
1 file changed +13
-9
lines changed Original file line number Diff line number Diff line change @@ -119,21 +119,25 @@ def rebuild!
119
119
end
120
120
121
121
def cleanup!
122
- ids_to_delete = [ ]
123
-
124
122
hierarchy_table = hierarchy_class . arel_table
125
123
124
+ query = hierarchy_class
125
+ alias_tables = [ ]
126
126
[ :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 ] ) )
129
132
. join_sources
130
133
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 )
136
135
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
137
141
end
138
142
end
139
143
end
You can’t perform that action at this time.
0 commit comments