|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | require "spec_helper"
|
| 4 | +require_relative "../has_and_belongs_to_many_models.rb" |
4 | 5 |
|
5 | 6 | describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
6 | 7 |
|
@@ -3770,4 +3771,33 @@ class Distributor
|
3770 | 3771 | expect(p2.d_ids).to match_array([d2.id])
|
3771 | 3772 | end
|
3772 | 3773 | end
|
| 3774 | + |
| 3775 | + # This test is for MONGOID-5344 which tests that the initial call to |
| 3776 | + # signature_ids refers to the same array as subsequent calls to signature_ids. |
| 3777 | + # Prior to the change in that ticket, this test broke because the array |
| 3778 | + # returned from write_attribute (which is triggered the first time the |
| 3779 | + # foreign key array is referenced, to set the default), refers to a different |
| 3780 | + # array to the one stored in the attributes hash. This happened because, |
| 3781 | + # when retrieving a document from the database, the attributes hash is actually |
| 3782 | + # a BSON::Document, which applies a transformation to the array before |
| 3783 | + # storing it. |
| 3784 | + context "when executing concat on foreign key array from the db" do |
| 3785 | + config_override :legacy_attributes, false |
| 3786 | + |
| 3787 | + before do |
| 3788 | + HabtmmContract.create! |
| 3789 | + HabtmmSignature.create! |
| 3790 | + end |
| 3791 | + |
| 3792 | + let!(:contract) { HabtmmContract.first } |
| 3793 | + let!(:signature) { HabtmmSignature.first } |
| 3794 | + |
| 3795 | + before do |
| 3796 | + contract.signature_ids.concat([signature.id]) |
| 3797 | + end |
| 3798 | + |
| 3799 | + it "works on the first attempt" do |
| 3800 | + expect(contract.signature_ids).to eq([signature.id]) |
| 3801 | + end |
| 3802 | + end |
3773 | 3803 | end
|
0 commit comments