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

Already on GitHub? Sign in to your account

save! fails silently with unique indexes #166

Closed
chadrem opened this Issue Jan 21, 2010 · 4 comments

Comments

Projects
None yet
4 participants

chadrem commented Jan 21, 2010

http://gist.github.com/282556 demonstrates the problem. In summary, I create new documents and save both of them. Both save! method calls return true, but only the first one actually saved. The second one fails silently. Lines 28, 30, and 34 demonstrate the problem. Lines 45 - 63 show the indexes (created by calling MongoMapper.ensure_indexes!).

chadrem commented Jan 21, 2010

It turns out the mongo ruby driver's collection.save() method requires the option :safe => true in order for unique indexes to raise an error. This appears to be because "an extra round-trip to the database" is required to check for errors. I didn't expect this to be the default behavior of mongomapper's .save! method.

Contributor

jnunemaker commented Jan 24, 2010

save! now takes options so you can pass safe in. Might switch the ! methods (create!, save! etc.) to just have :safe on by default. Closed by e21a6c1.

It was very annoying with this silent failures. 10x

Contributor

smtlaissezfaire commented Jun 26, 2015

Another option is to use safe in your model:

module User
safe
end

(see safe.rb in the examples directory)

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment