Refactor the logic that checks whether or not to emit the hidden id field

By checking for object.persisted? first, we avoid the hash lookups for
new objects.