key.to_key_hash().dup is not the same as key.attributes.dup - intentional?  Also, there's no need to dup to_key_hash - it always creates a new object (unless you meant to deep_dup, which I would argue to_key_hash should be doing if it 's important)