I'm developing an app in which users can create entries. Each entry needs a unique identifier. That identifier consists of 2 values:
- A user unique identifier code (ex: ZZ)
- The amount of entries the user has entered
So, if there's a user whose identifier is "ZZ", his entries will be identified by the following ids: "ZZ1", "ZZ2", "ZZ3" and so on.
Other users would have different identifier codes. And their entry count should be independent, even when they are all stored in the same table.
My first attempt was adding a "user_code" field in the users table, and the entry count was a scope in the model that counted the user entries. It does the job. But the problem arises when users delete their entries. The count gets messed up. Let's say that a user has 5 entries, but deletes the 4th entry. Then when a new he creates a new entry, it should be counted as the 6th entry. But since one entry was deleted, then the counter just counts 4 entries, instead of 5.
I'm looking for a better way to do this. Any ideas?
Thanks!