If I got it right, the expected behavior is to strip a trailing `_id` unless the whole string is `_id`. To accomplish that while keeping it backwards compatible we can use:

```ruby
result.sub!(/(?!^)_id$/, "")
```

Also, `gsub!` wasn't needed in the first place, because we are only interested in that single occurrence of `_id`. So we can safely simplify things and just call `sub!`.