Prefer "if any?" to "unless empty?"

* Consistent whitespace
* Remove unnecessary parentheses
Fix warnings for undefined local variable

* Add private method primary_key_for, which more clearly shows that the
expected return value is nil when a primary key is not found.