Skip to content

Commit 449022f

Browse files
authored
MONGOID-5379 Don't pass through the database_name to the collection options (#5339)
* MONGOID-5379 Don't pass through the database_name to the collection options * MONGOID-5379 consider string keys as well
1 parent 5d44bd4 commit 449022f

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/mongoid/persistence_context.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ def initialize(object, opts = {})
5757
# @return [ Mongo::Collection ] The collection for this persistence
5858
# context.
5959
def collection(parent = nil)
60-
parent ? parent.collection.with(client_options) : client[collection_name.to_sym]
60+
parent ?
61+
parent.collection.with(client_options.except(:database, "database")) :
62+
client[collection_name.to_sym]
6163
end
6264

6365
# Get the collection name for this persistence context.

spec/mongoid/persistence_context_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,4 +718,28 @@
718718
end
719719
end
720720
end
721+
722+
context "when using an alternate database to update a document" do
723+
let(:user) do
724+
User.new(name: '1')
725+
end
726+
727+
before do
728+
user.with(database: database_id_alt) do |u|
729+
u.save!
730+
end
731+
732+
expect do
733+
user.with(database: database_id_alt) do |u|
734+
u.update(name:'2')
735+
end
736+
end.to_not raise_error
737+
end
738+
739+
it "persists the update" do
740+
User.with("database" => database_id_alt) do |klass|
741+
expect(klass.find(user._id).name).to eq("2")
742+
end
743+
end
744+
end
721745
end

0 commit comments

Comments
 (0)