Tigraine

Daniel Hoelbling-Inzko talks about programming

Mongoid gotcha: Id equality comparisons

Posted by Daniel Hölbling on July 9, 2013

One quick thing I just ran into in my Rails app with Mongoid:

Ids in model instances are not Strings objects and thus their == method doesn't work against normal Strings.

This means:

@post.id == '51abcb7b421aa90cdf000146'   # always false

will never be true unless you explicitly cast the ObjectId to a string:

@post.id.to_s == '51abcb7b421aa90cdf000146'  # correct comparison

Oh and yes: you shouldn't do things like that anyways in your code - but if you have to - be aware.

Filed under mongoid, rails, mongodb
comments powered by Disqus

My Photography business

Projects

dynamic css for .NET

Archives

more