문제

I'm attempting to create what is essentially a second id column for a nested route in Rails.

I have a route that looks something like this: /projects/:project_id/bugs/:bug_id.

Projects have a has_many :bugs relationship and a bug belongs_to :project. Right now the bug_id shown in the route is the standard incremental id that is generated when a model is saved to the database.

However, I would prefer it to be relative to the project.

In other words, I'd like to create a column called relative_id on the bug model which starts from 1 inside every project and increments automatically.

Example:

/projects/1/bugs/1 #should take me to the first bug of project one
/projects/2/bugs/1 #should take me to the first bug of project two

My question is, do I have to maintain this relative_id column manually or is there some Rails 'magic' that I can leverage to make this easier?

routes.rb:

resources :projects do
    get 'bugs/feed' => 'bugs#feed'
    get 'bugs/groups' => 'bugs#groups'
    resources :bugs
    resources :permissions, :only => ['create']
end
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top