【问题标题】:Saving Issue Custom Field Values via Rake Task in Redmine通过 Redmine 中的 Rake 任务保存问题自定义字段值
【发布时间】:2020-03-25 00:08:04
【问题描述】:

您好,我有一个 rake 任务,当通过控制台工作执行时,它在调用 rake 任务时不起作用。实际上它确认发生了保存,而值没有更新。似乎缺少一些活动记录提交,因为我看到的日志中唯一的区别是如果运行(通过控制台)开始提交转储,而当作为 rake 任务运行时,Redmine 的 debug.log 中没有这样的部分......

这是它的内容:

namespace :redmine do
  namespace :myplugin do
    desc 'Updates custom field for idle tickets.'
    task :update_idle => :environment do
      Issue.open.each do |issue|
        begin
          days = DateTime.now.to_time - DateTime.parse(issue.updated_on.to_s).to_time
          days = days/86400
          if days > 0
            cfv = issue.custom_field_values.detect {|cfv| cfv.custom_field_id == 19 }
            cfv.value = days.ceil
            if issue.save
              puts "#{issue.id} saved"
            else
              puts "#{issue.id} not saved"
            end
          end
        end
      end
    end
  end
end

【问题讨论】:

  • 如果您不更改问题属性而只更改自定义字段值,则有save_custom_field_values方法,在acts_as_customizable模块中定义

标签: ruby-on-rails ruby rake redmine redmine-plugins


【解决方案1】:

上述代码的问题在于,它在 Redmine 3.4 中既可以在控制台中工作,也可以作为 Rake 任务工作,但在 Redmine 4 中,它只能在控制台中工作。

最终我能够通过 Rake 任务在 Redmine 4 上通过以下方式对问题的自定义字段执行更改:

namespace :redmine do
  namespace :myplugin do
    desc 'Updates custom field for idle tickets.'
    task :update_idle => :environment do
      Issue.open.each do |issue|
        begin
          days = DateTime.now.to_time - DateTime.parse(issue.updated_on.to_s).to_time
          days = days/86400
          if days > 0
            issue.custom_values.each do |x|
              if x.custom_field_id == 19
                x.value = days.ceil
                x.save
              end
             end
            issue.save
            true
          end
        end
      end
    end
  end
end

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    相关资源
    最近更新 更多