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