From be0cfc07ec13d6f72a3ee95999053fe61aa93da1 Mon Sep 17 00:00:00 2001 From: Shutkov Maksim Date: Wed, 9 Sep 2015 21:04:34 +0300 Subject: [PATCH] Improved cascade updae tree --- Gemfile.lock | 3 --- lib/pg_ltree/ltree.rb | 6 ++++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 0e7e4cc..3f72c1b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -106,6 +106,3 @@ PLATFORMS DEPENDENCIES pg_ltree! yard - -BUNDLED WITH - 1.10.5 diff --git a/lib/pg_ltree/ltree.rb b/lib/pg_ltree/ltree.rb index fc285f7..f9ead46 100644 --- a/lib/pg_ltree/ltree.rb +++ b/lib/pg_ltree/ltree.rb @@ -191,8 +191,10 @@ def children # # @return [ActiveRecord::Relation] def cascade_update - ltree_scope.where(["#{ltree_path_column} <@ ?", ltree_path_was]).where(["#{ltree_path_column} != ?", ltree_path]). - update_all ["#{ltree_path_column} = ? || subpath(#{ltree_path_column}, nlevel(?))", ltree_path, ltree_path_was] + if ltree_path_was.include? self.id.to_s + ltree_scope.where(["#{ltree_path_column} <@ ?", ltree_path_was]).where(["#{ltree_path_column} != ?", ltree_path]). + update_all ["#{ltree_path_column} = ? || subpath(#{ltree_path_column}, nlevel(?))", ltree_path, ltree_path_was] + end end # Delete all children for current path