I would recommend against this, because it's only going to make your life more difficult. For example, instead of being able to do Bug.find(2)
, you're always going to have to do Bug.where(project_id: 5, relative_id: 2)
or Project.find(5).bugs.where(relative_id: 2)
. That might not seems like a big deal but it'll get in your way more often than you might realize.
If you use a regular "global" AUTO_INCREMENT id for bugs
you can still have pretty URLs like /projects/5/bugs/2
(and you might want to anyway if e.g. only someone who has access to project X should be able to access its bugs). Is it important to your users that a new project's bug IDs always start at 1?
If you decide you really need to do it I'm pretty sure you'll need to implement it on your own. Probably doing it at the database level (instead of in Rails) would be best, just for consistency reasons. You'll need to do it such that you get the previous MAX(relative_id)
and create the new record in such a way that no other records can be created in the meantime (or you'll end up with relative_id
collisions). Here's a related question that might help you out: MySQL second auto increment field based on foreign key
I know that's not exactly the answer you're looking for, but I hope it's helpful.