-1

How can you do a numbering scheme like Github does with repositories issues and pull requests inside a project (1, 2, 3, ..., n)?

Assume we have a Project model and also have a Post model, where Post belongs_to Project .

I could imagine looking for the last Post for a given project and incrementing that number, but I think this could easily lead to a race condition.

Josh Smith
  • 14,674
  • 18
  • 72
  • 118
  • why not just using an `id`? – Andrey Deineko Dec 01 '15 at 18:26
  • @AndreyDeineko perhaps I didn't explain well and relied on Github to do the explaining for me. The idea is that each project has its own specific numbering. That is, Project #1 has `1, 2, 3`, Project #2 has `1, 2, 3` and so on. With an auto-incrementing `id` you would have Project #1 have `1`, and maybe Project #2 has `2, 3`, and Project #1 has `4, 5, 6`. This would be a loss of continuity for the user's experience. – Josh Smith Dec 01 '15 at 19:08

1 Answers1

0

Assuming that you have..

class Post < ActiveRecord::Base
  belongs_to :project
end

One possible solution is to add a has_many in your Project model

class Project < ActiveRecord::Base
  has_many :posts
end

and use the size property

proj = Project.find( ... )

proj.posts.size