Updating all issues that have a certain value for a custom field
Added by Andreas Bloch about 3 years ago
I have the following code that allows me to bulk-update all issues that are in a certain status (1) to another status (13):
Project.find_by(identifier: 'project-a').issues.where(status_id: 1).update_all(status_id: 13)
I would like to do the same thing, but only for issues that have a certain value 'A' for a custom field (number 70).
Project.find_by(identifier: 'project-a').issues.where(status_id: 1).select{|issue| issue.custom_field_value(70) == 'A'}.update_all(status_id: 13)
Unfortunately, the following code does not work, because 'select' transforms the elements into simple array objects that are no longer synched with the DB.
Therefore, 'update_all' cannot be applied (and throws an error).
Do you know a way how I could further filter the issues according to a certain value of their custom fields and then bulk-update them?
Ideally, the approach would not load all the issues into memory and then interate over them.
Replies (1)
RE: Updating all issues that have a certain value for a custom field - Added by Andreas Bloch about 3 years ago
So far I was able to do the following (which works, but is not so elegant, because it does not stick to the API, but relies on SQL).
Project.find_by(identifier: 'project-a').issues.joins(:custom_values).where("custom_field_id = 70 AND value='A'").update_all(status_id: 13)