diff --git a/app/models/relationship.rb b/app/models/relationship.rb index 9babad135e68..6ba8ec6d8685 100644 --- a/app/models/relationship.rb +++ b/app/models/relationship.rb @@ -197,7 +197,8 @@ def self.stringify_resource_pairs(resource_pairs, options = {}) def grandchild_conditions t = self.class.arel_table t.grouping(t[:ancestry].matches("#{child_ancestry}/%", nil, true).and( - t[:ancestry].does_not_match("#{child_ancestry}/%", nil, true))) + t[:ancestry].does_not_match("#{child_ancestry}/%/%", nil, true)) + ) end def child_and_grandchild_conditions diff --git a/spec/models/mixins/relationship_mixin_spec.rb b/spec/models/mixins/relationship_mixin_spec.rb index 41dff5cab217..f12bc60400c8 100644 --- a/spec/models/mixins/relationship_mixin_spec.rb +++ b/spec/models/mixins/relationship_mixin_spec.rb @@ -813,7 +813,7 @@ end end - describe "#parent_rel_ids" do + describe "#parent_rels" do it "works with relationships" do pars = vms[8].with_relationship_type(test_rel_type, &:parent_rels) pars_vms = pars.map(&:resource) @@ -839,6 +839,24 @@ end end + describe "#grandchild_rels" do + it "works with relationships" do + vms[0].with_relationship_type(test_rel_type) do + rels = vms[0].grandchild_rels + expect(rels.map(&:resource)).to match_array([vms[3], vms[4], vms[5], vms[6], vms[7]]) + end + end + end + + describe "#child_and_grandchild_rels" do + it "works with relationships" do + vms[0].with_relationship_type(test_rel_type) do + rels = vms[0].child_and_grandchild_rels + expect(rels.map(&:resource)).to match_array([vms[1], vms[2], vms[3], vms[4], vms[5], vms[6], vms[7]]) + end + end + end + protected def build_relationship_tree(tree, rel_type = test_rel_type, base_factory = :vm_vmware)