changing a Set key does not mark "changed?"

12 views
Skip to first unread message

Jérémy Lecour

unread,
May 18, 2011, 12:43:36 PM5/18/11
to mongo...@googlegroups.com
Hi,

I'm trying to track changes before saving a document.

I have a Set key, with some members. After a find, if I add a member to the set, the document is not marked as changed.
If I change a String or Integer key, it's working
Is it normal or am I missing something ?

Here is an IRB session :

>> s = Search.last #=> (...)
>> s.location_ids
=> #<Set: {324339, 11960, 351452, 350236, 2536, 2584, 4579, 5206, 12103}>
>> s.changed? #=> false
>> s.location_ids << 123
=> #<Set: {123, 324339, 11960, 351452, 350236, 2536, 2584, 4579, 5206, 12103}>
>> s.changed? #=> false
>> s.remote_ip #=> nil
>> s.remote_ip = '127.0.0.1' #=> "127.0.0.1"
>> s.changed? #=> true
>> s.changes #=> {"remote_ip"=>[nil, "127.0.0.1"]}

Nathan Stults

unread,
May 18, 2011, 1:24:59 PM5/18/11
to mongo...@googlegroups.com

You’re not missing anything – MM does not have a capability for detecting changes on objects modified in place like that. It can only detect changes when the key is assigned using doc.key = “whatever”

 

s.remote_ip << “.1” wouldn’t work either.

 

Getting MM to track changes made in place to object keys is a pretty big job. I’ve tried (see mm-dirtier on github) but it’s still very much a work in progress. There are still a number of edge cases that simply don’t work. I would think that for the moment your best bet might be doing a diff of the attributes hash before you save. I don’t know what kind of performance implication that would have, but given the difficulty of the alternative, I may have to switch my projects to an approach like that ultimately as well.

 

Nathan

--
You received this message because you are subscribed to the Google
Groups "MongoMapper" group.
For more options, visit this group at
http://groups.google.com/group/mongomapper?hl=en?hl=en

Jérémy Lecour

unread,
May 18, 2011, 3:03:23 PM5/18/11
to MongoMapper
Hi Nathan

Thanks for the answer.

I've tried this, but it doesn't work either :

>> s.location_ids += [123]

I guess you're right, I'll explicitly check those "complex" types.
Reply all
Reply to author
Forward
0 new messages