Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MONGOID-5379 Don't pass through the database_name to the collection options #5339

Merged
merged 2 commits into from Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/mongoid/persistence_context.rb
Expand Up @@ -57,7 +57,9 @@ def initialize(object, opts = {})
# @return [ Mongo::Collection ] The collection for this persistence
# context.
def collection(parent = nil)
parent ? parent.collection.with(client_options) : client[collection_name.to_sym]
parent ?
parent.collection.with(client_options.except(:database, "database")) :
client[collection_name.to_sym]
end

# Get the collection name for this persistence context.
Expand Down
24 changes: 24 additions & 0 deletions spec/mongoid/persistence_context_spec.rb
Expand Up @@ -718,4 +718,28 @@
end
end
end

context "when using an alternate database to update a document" do
let(:user) do
User.new(name: '1')
end

before do
user.with(database: database_id_alt) do |u|
u.save!
end

expect do
user.with(database: database_id_alt) do |u|
u.update(name:'2')
end
end.to_not raise_error
end

it "persists the update" do
User.with("database" => database_id_alt) do |klass|
expect(klass.find(user._id).name).to eq("2")
end
end
end
end