diff --git a/app/models/placement.rb b/app/models/placement.rb index 08d7f009f..410f44800 100644 --- a/app/models/placement.rb +++ b/app/models/placement.rb @@ -22,7 +22,8 @@ # fk_rails_... (subject_id => subjects.id) # class Placement < ApplicationRecord - after_save :assign_subject # TODO: Remove when data migrated + # TODO: Remove when data migrated + after_save :assign_subject, unless: proc { |placement| placement.subject.present? } has_many :placement_mentor_joins, dependent: :destroy has_many :mentors, through: :placement_mentor_joins, class_name: "Placements::Mentor" @@ -58,9 +59,7 @@ def self.order_by_school_ids(school_ids) # TODO: Remove after data migrated def assign_subject - return if subjects.blank? || - subject.present? || - subject == subjects.last + return if subjects.blank? update!(subject: subjects.last) end diff --git a/spec/factories/placements/placement_additional_subjects.rb b/spec/factories/placements/placement_additional_subjects.rb index 41bad10c4..df3f03bc3 100644 --- a/spec/factories/placements/placement_additional_subjects.rb +++ b/spec/factories/placements/placement_additional_subjects.rb @@ -20,5 +20,7 @@ # FactoryBot.define do factory :placement_additional_subject, class: "Placements::PlacementAdditionalSubject" do + association :subject + association :placement end end diff --git a/spec/models/placement_spec.rb b/spec/models/placement_spec.rb index 9e6a306bf..d4e091887 100644 --- a/spec/models/placement_spec.rb +++ b/spec/models/placement_spec.rb @@ -110,8 +110,8 @@ end end - context "when a subject has been associated with the placement" do - context "when the subject is not same as the already associated subject" do + context "when subjects have been associated with the placement" do + context "when subject is nil" do let(:subjects) { [create(:subject)] } let(:a_subject) { nil } @@ -120,9 +120,9 @@ end end - context "when the subject is same as the already associated subject" do + context "when subject is not nil" do let(:subjects) { [create(:subject)] } - let(:a_subject) { subjects.last } + let(:a_subject) { create(:subject) } it "keeps the original subject assigned to the placement" do expect(placement.subject).to eq(a_subject)