Skip to content

Broken == in MongoMapper

I am hoping to bring a few more eyes to an equality bug I found in MongoMapper:


p1 = Post.new
p2 = p1.dup
p1 == p2 # => true
# so far so good
p2.title = 'changed'
p1 == p2 # => false
# uh-oh!

If you want to see chaos, please join us for a spirited mailing list discussion. Despite my best efforts, it seems to be spiraling out of control.

Update as of 5:55pm: ActiveRecord gets it wrong, too, but in a different way:


p1 = Post.new
p2 = p1.dup
p1 == p2 # => false
# drat!
p2.title = 'changed'
p1 == p2 # => false
# double drat!

Update as of 6:08pm: DataMapper is the only one that gets it right:


p1 = Post.new
p2 = p1.dup
p1 == p2 # => true
# yay!
p2.title = "x"
p1 == p2 # => false
# yay!

3 Comments

  1. Dan Kubb wrote:

    FWIW, I agree with you in the above case.

    In DataMapper we decided that by default objects should be compared based on their public attributes values, not internal bookkeeping values. This is true even in cases where the two objects may refer to the same record in the datastore.

    Here’s a link to the relevant specs we have: http://bit.ly/4cE7EZ

    Although I wouldn’t say the conversation is spiraling out of control. It appears to be reasonably civil, but I understand how it could appear that way when you have a strong opinion of how something should work. I’ve certainly been involved in discussions that didn’t feel good at the time, but looking back they weren’t that bad.. in reality these types of discussions are good for the community because it gets people to pay attention to the details.

    Wednesday, October 21, 2009 at 5:25 pm | Permalink
  2. Dan Kubb wrote:

    I just checked the DataMapper example above, and the last statement returns false with the edge DM.

    Script to reproduce: http://gist.github.com/215552

    Wednesday, October 21, 2009 at 5:53 pm | Permalink
  3. David wrote:

    Dan: Whoops! I fixed the typo. DataMapper behaves like I would expect, thanks!

    Wednesday, October 21, 2009 at 5:58 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*