-
Type: Bug
-
Resolution: Done
-
Priority: Minor - P4
-
Affects Version/s: 5.1.2
-
Component/s: None
-
None
Hello guys,
my issue is a bit linked to fix introduced in https://jira.mongodb.org/browse/MONGOID-4137. Mentioned fix does not handle use case when the document already exists but doesn't respond to `substitute`. In this case method fixed returns just `nil` and surrouding code is not prepared for such a case.
Let me present an example of failing code based on the `builders_spec.rb` in the repo:
```
context "when the parent is persisted" do
let(:person) do
Person.create
end
context "when the relation is a has one" do
context "when a document already exists" do
context "when the relation belongs to is set" do
let!(:game_one) do
game = person.create_game(name: "Starcraft")
game.person = person
end
let!(:game_two) do
person.create_game(name: "Skyrim")
end
it "replaces the existing document" do
expect(person.game).to eq(game_two)
end
it "persists the change" do
expect(person.game(true)).to eq(game_two)
end
it "removes the old document from the database" do
expect(Game.collection.find.count).to eq(1)
end
end
end
end
end
```
This raises an error
```
NoMethodError: undefined method `save' for nil:NilClass
./lib/mongoid/relations/builders.rb:97:in `block in creator'
```
I am not sure what should be the intended behaviour? Should be mongoid able to substitute instances even in this use case or should it raise some custom Mongoid error?
Any help appreciated!
Thanks,
Jan