Skip to content

Commit

Permalink
Fix policy_events relationship on VmOrTemplate
Browse files Browse the repository at this point in the history
The policy_events relationship currently results in an internal server error because it is referencing "id" which is not valid

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1546995
  • Loading branch information
Jillian Tullo committed Feb 26, 2018
1 parent f20008d commit adc676c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/vm_or_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class VmOrTemplate < ApplicationRecord
has_many :ems_events_src, :class_name => "EmsEvent"
has_many :ems_events_dest, :class_name => "EmsEvent", :foreign_key => :dest_vm_or_template_id

has_many :policy_events, -> { where(["target_id = ? OR target_class = 'VmOrTemplate'", id]).order(:timestamp) }, :class_name => "PolicyEvent"
has_many :policy_events, ->(vm) { where(["target_id = ? AND target_class = 'VmOrTemplate'", vm.id]).order(:timestamp) }, :foreign_key => "target_id"

has_many :miq_events, :as => :target, :dependent => :destroy

Expand Down
8 changes: 8 additions & 0 deletions spec/models/vm_or_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1229,4 +1229,12 @@
expect(Vm.new(:location => "test location", :storage => storage).v_datastore_path).to eq("storage name/test location")
end
end

context "#policy_events" do
it "returns the policy events with target class of VmOrTemplate and target_id of the vm" do
policy_event = FactoryGirl.create(:policy_event, :target_class => "VmOrTemplate", :target_id => vm.id)

expect(vm.policy_events).to eq([policy_event])
end
end
end

0 comments on commit adc676c

Please sign in to comment.