Rails: auto-zoom column with scope

I have projects and problems. The problem number (not an identifier) ​​should be auto-incremented, but with scope with project_id. Any stone or easy way to do this? Thanks!

Edit: act_as_list will complete the task, but what if the record is deleted? The next number will be with this number.

+4
source share
2 answers

in the class "Problem":

belongs_to :project before_validation( :on => :create ) do self.issue_number = self.project.issues.collect { | issue | issue.issue_number }.max + 1 end 

(or so) - essentially, before creating a new object, find the maximum number of problems associated with the project of the problem, increase this value and use it for the new problem number ...

+2
source

You can use counter_cache to communicate with projects and override Project decment_counter to do nothing.

0
source

Source: https://habr.com/ru/post/1390781/


All Articles