File tree 2 files changed +5
-10
lines changed
2 files changed +5
-10
lines changed Original file line number Diff line number Diff line change @@ -121,23 +121,18 @@ def rebuild!
121
121
def cleanup!
122
122
hierarchy_table = hierarchy_class . arel_table
123
123
124
- query = hierarchy_class
125
- alias_tables = [ ]
126
124
[ :descendant_id , :ancestor_id ] . each do |foreign_key |
127
125
alias_name = foreign_key . to_s . split ( '_' ) . first + "s"
128
126
alias_table = Arel ::Table . new ( table_name ) . alias ( alias_name )
129
- alias_tables << alias_table
130
127
arel_join = hierarchy_table . join ( alias_table , Arel ::Nodes ::OuterJoin )
131
128
. on ( alias_table [ primary_key ] . eq ( hierarchy_table [ foreign_key ] ) )
132
129
. join_sources
133
130
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 )
136
133
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
141
136
end
142
137
end
143
138
end
Original file line number Diff line number Diff line change 137
137
138
138
add_foreign_key ( :metal , :metal , :column => 'parent_id' )
139
139
140
- create_table "metal_hierarchies" do |t |
140
+ create_table "metal_hierarchies" , :id => false do |t |
141
141
t . integer "ancestor_id" , :null => false
142
142
t . integer "descendant_id" , :null => false
143
143
t . integer "generations" , :null => false
You can’t perform that action at this time.
0 commit comments