Reputation: 2929
I have projects and issues. The issue number (not ID) must be auto-increment but scoped with project_id. Any gem or easy way to accomplish that? Thank you!
Edit: acts_as_list will do the job, but what if a record is deleted? The next issue will be with it's number.
Upvotes: 6
Views: 2832
Reputation: 9840
You could use the counter_cache on the Project issues association and override Project's decrement_counter to do nothing.
Upvotes: 0
Reputation: 2924
in your Issue class:
belongs_to :project
before_validation( :on => :create ) do
self.issue_number = self.project.issues.collect { | issue | issue.issue_number }.max + 1
end
(or thereabouts) -- essentially, before the new object is created, find the max issue number of the issues associated with the issue's project, increment that, and use it for the new issue number...
Upvotes: 3