File tree Expand file tree Collapse file tree 2 files changed +5
-10
lines changed Expand file tree Collapse file tree 2 files changed +5
-10
lines changed Original file line number Diff line number Diff line change @@ -121,23 +121,18 @@ def rebuild!
121121 def cleanup!
122122 hierarchy_table = hierarchy_class . arel_table
123123
124- query = hierarchy_class
125- alias_tables = [ ]
126124 [ :descendant_id , :ancestor_id ] . each do |foreign_key |
127125 alias_name = foreign_key . to_s . split ( '_' ) . first + "s"
128126 alias_table = Arel ::Table . new ( table_name ) . alias ( alias_name )
129- alias_tables << alias_table
130127 arel_join = hierarchy_table . join ( alias_table , Arel ::Nodes ::OuterJoin )
131128 . on ( alias_table [ primary_key ] . eq ( hierarchy_table [ foreign_key ] ) )
132129 . join_sources
133130
134- query = query . joins ( arel_join )
135- end
131+ lonely_childs = hierarchy_class . joins ( arel_join ) . where ( alias_table [ primary_key ] . eq ( nil ) )
132+ ids = lonely_childs . pluck ( foreign_key )
136133
137- query . where (
138- alias_tables . first [ primary_key ] . eq ( nil )
139- . or ( alias_tables . second [ primary_key ] . eq ( nil ) )
140- ) . destroy_all
134+ hierarchy_class . where ( hierarchy_table [ foreign_key ] . in ( ids ) ) . delete_all
135+ end
141136 end
142137 end
143138 end
Original file line number Diff line number Diff line change 137137
138138 add_foreign_key ( :metal , :metal , :column => 'parent_id' )
139139
140- create_table "metal_hierarchies" do |t |
140+ create_table "metal_hierarchies" , :id => false do |t |
141141 t . integer "ancestor_id" , :null => false
142142 t . integer "descendant_id" , :null => false
143143 t . integer "generations" , :null => false
You can’t perform that action at this time.
0 commit comments