Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Use $set to only alter specified fields in Mongo document on update #31

Merged
merged 1 commit into from
Sep 27, 2016

Conversation

nporteschaikin
Copy link
Contributor

This addresses the issue @pbrisbin raised in codeclimate/app#2919, where calling Minidoc#update overwrites the entire Mongo document in lieu of only updating fields specified by the model.

cc @codeclimate/review

@nporteschaikin nporteschaikin force-pushed the npc/save-dont-overwrite-attributes branch from 7da7b10 to e54da1d Compare September 26, 2016 19:22
@pbrisbin
Copy link
Contributor

Awesome. I didn't realize it would be simple to address.

@nporteschaikin nporteschaikin force-pushed the npc/save-dont-overwrite-attributes branch from e54da1d to a4fa377 Compare September 26, 2016 19:30
@nporteschaikin
Copy link
Contributor Author

@@ -234,7 +234,7 @@ def create
end

def update
self.class.collection.update({ _id: id }, attributes)
self.class.collection.update({ _id: id }, { "$set" => attributes.except!(:_id) })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about using except instead of except! to avoid mutation? I'm not sure if it will have side-effects but I worry it might, and it doesn't seem necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxjacobson not sure why I went with the mutable array - fixed on rebase.

@nporteschaikin nporteschaikin force-pushed the npc/save-dont-overwrite-attributes branch from a4fa377 to ce7cf8b Compare September 26, 2016 19:37
@@ -7,10 +7,18 @@
I18n.load_path << File.expand_path("../locale/en.yml", __FILE__)

class User < Minidoc
self.collection_name = "users"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed on the base class here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wfleming no - just wanted to be explicit so it's clear in the code why we're using two classes.

Copy link
Contributor

@wfleming wfleming Sep 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not something we'd do in normal code, so I don't think it's necessary here in the test either. The other class is right below it, after all. NBD if you want to keep it, though.

@nporteschaikin nporteschaikin changed the title Use $set to only alter update specified fields in Mongo document Use $set to only alter specified fields in Mongo document on update Sep 26, 2016
@wfleming
Copy link
Contributor

LGTM

@nporteschaikin nporteschaikin merged commit af25354 into master Sep 27, 2016
@nporteschaikin nporteschaikin deleted the npc/save-dont-overwrite-attributes branch September 27, 2016 21:26
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants