diff --git a/app/models/relationship.rb b/app/models/relationship.rb index f6b28a50e..0f0433f13 100644 --- a/app/models/relationship.rb +++ b/app/models/relationship.rb @@ -162,7 +162,7 @@ def self.create_unless_exists(source_id, target_id, relationship_type, options = r.update_options(options) unless r.nil? return r unless r.nil? # otherwise we should get the existing relationship based on source, target and type - r = Relationship.where(source_id: source_id, target_id: target_id).where('relationship_type = ?', relationship_type.to_yaml).last + r = Relationship.where(source_id: source_id, target_id: target_id).last exception_message = nil exception_class = nil if r.nil? @@ -198,6 +198,10 @@ def self.create_unless_exists(source_id, target_id, relationship_type, options = exception_class = e.class.name end else + if r.relationship_type != relationship_type && relationship_type == Relationship.confirmed_type + r.relationship_type = relationship_type + r.save! + end r.update_options(options) end if r.nil? diff --git a/test/models/relationship_test.rb b/test/models/relationship_test.rb index 9ff193312..ca4620054 100644 --- a/test/models/relationship_test.rb +++ b/test/models/relationship_test.rb @@ -349,7 +349,7 @@ def setup assert_no_difference 'Relationship.count' do r2 = Relationship.create_unless_exists(source.id, target.id, Relationship.confirmed_type) end - assert_nil Relationship.where(id: r.id).last + assert_equal r.id, r2.id assert_equal Relationship.confirmed_type, r2.relationship_type Relationship.any_instance.stubs(:save!).raises(ActiveRecord::RecordNotUnique) target = create_project_media team: t