Always return correct primary key

The previous implementation failed when the primary key
had no sequence (eg UUID)

Used the code from rails 3.2