From 057fb9c72d2624e5b9ff825217b6eba5deecafd0 Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Thu, 22 Aug 2019 11:25:10 -0400 Subject: [PATCH] Fix the ems_events relation to return src and dest The query for vm.ems_events wasn't returning any events where the vm was the dest_vm_or_template_id because an extra AND ems_event.vm_or_template_id = id was being added to the where query. --- app/models/vm_or_template.rb | 5 +++-- spec/models/vm_or_template_spec.rb | 15 +++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/models/vm_or_template.rb b/app/models/vm_or_template.rb index cee8511b3b64..bac6569c34ca 100644 --- a/app/models/vm_or_template.rb +++ b/app/models/vm_or_template.rb @@ -120,8 +120,9 @@ class VmOrTemplate < ApplicationRecord has_one :openscap_result, :as => :resource, :dependent => :destroy # EMS Events - has_many :ems_events, ->(vmt) { where(["vm_or_template_id = ? OR dest_vm_or_template_id = ?", vmt.id, vmt.id]).order(:timestamp) }, - :class_name => "EmsEvent" + def ems_events + EmsEvent.where("vm_or_template_id = ? OR dest_vm_or_template_id = ?", id, id).order(:timestamp) + end has_many :ems_events_src, :class_name => "EmsEvent" has_many :ems_events_dest, :class_name => "EmsEvent", :foreign_key => :dest_vm_or_template_id diff --git a/spec/models/vm_or_template_spec.rb b/spec/models/vm_or_template_spec.rb index 1b1edd402084..89c1b746452f 100644 --- a/spec/models/vm_or_template_spec.rb +++ b/spec/models/vm_or_template_spec.rb @@ -619,12 +619,15 @@ end it "with ems_events" do - ems = FactoryBot.create(:ems_vmware_with_authentication) - vm = FactoryBot.create(:vm_vmware, :ext_management_system => ems) - ems_event = FactoryBot.create(:ems_event) - vm.ems_events << ems_event - expect(vm.ems_events.first).to be_kind_of(EmsEvent) - expect(vm.ems_events.first.id).to eq(ems_event.id) + ems = FactoryBot.create(:ems_vmware_with_authentication) + vm = FactoryBot.create(:vm_vmware, :ext_management_system => ems) + ems_event_src = FactoryBot.create(:ems_event, :vm_or_template => vm) + ems_event_dest = FactoryBot.create(:ems_event, :dest_vm_or_template => vm) + + expect(vm.ems_events.count).to eq(2) + expect(vm.ems_events_src.first).to be_kind_of(EmsEvent) + expect(vm.ems_events_src.first.id).to eq(ems_event_src.id) + expect(vm.ems_events_dest.first.id).to eq(ems_event_dest.id) end it "#miq_provision_vms" do