我正在使用paper_trail gem 对我的模型进行版本控制。
到目前为止,我的模型取决于以下info_for_paper_trail
方法ApplicationController
:
class ApplicationController < ActionController::Base
# Extra columns to store along with PaperTrail `versions`
def info_for_paper_trail
{ revision_id: @revision.id, revision_source_id: @revision_source.id }
end
end
这在控制器的上下文中效果很好,但是有没有办法可以在控制器的上下文之外复制这种事情(例如,延迟的工作)?
我尝试创建一个名为revision
并将 a 传递给的虚拟属性proc
into has_paper_trail
,但它出错并出现method not found
异常:
# Attempt to solve this in the model
class Resource < ActiveRecord::Base
# Virtual attribute
attr_accessor :revision
# Attempt to use virtual attribute only if set from delayed job
has_paper_trail meta: proc { |resource| resource.revision.present? ? { revision_id: resource.revision.id, revision_source_id: revision.revision_source.id } : {} }
end
# Gist of what I'm trying to do in the delayed job
resource = Resource.new
resource.revision = Revision.new(user: user, revision_source: revision_source)
resource.save!
我假设基于这个meta
不能接受的结果proc
,而且我不喜欢这个解决方案的味道......