编辑 paper_trail 个版本而不创建新版本
Edit a paper_trail version without creating a new version
我在 Rails 3.2 应用程序上使用 paper_trail 3.0.8,我有一个名为 'levels' 的模型,我保留了这些级别的版本。每个级别都有一个 from_date 和与之相关的成本。每当有人更改日期时,都会创建新版本。
我允许人们根据需要删除旧版本,这很有效。我希望能够修改旧 paper_trail 版本并在不创建新版本的情况下保存它。
class Level < ActiveRecord::Base
has_paper_trail :only => [:from_date],
:if => Proc.new { |l|
l.versions.count == 0 || l.versions.first.item != nil && (l.versions.first.item.from_date.nil? || l.from_date > l.versions.first.item.from_date)
}
<snip code>
end
如果我执行以下操作,它只会更新当前级别而不是版本
level = Level.find 1
version=level.versions[1].reify
version.cost_cents = 1000
version.save
是否有更新旧版本的 cost_cents?
还有没有办法在不创建新版本的情况下更新旧版本的 from_date?
Is there anyway to update the cost_cents for an old version?
是的,但我唯一知道的方法有点尴尬。
PaperTrail::Version
是一个普通的 ActiveRecord
对象,所以从这个意义上讲它很容易使用,但是数据是序列化的(在 YAML 中,默认情况下)所以你必须去 -序列化,进行更改,然后重新序列化。
v = PaperTrail::Version.last
hash = YAML.load(v.object)
hash[:my_attribute] = "my new value"
v.object = YAML.dump(hash)
v.save
使用 ActiveRecord 的自动序列化功能(如 ActiveRecord::AttributeMethods::Serialization
)可能有更好的方法来执行此操作。
PS:我看到您正在尝试使用 reify
,其中 returns 是您模型的实例,而不是 PaperTrail::Version
.[=17= 的实例]
我在 Rails 3.2 应用程序上使用 paper_trail 3.0.8,我有一个名为 'levels' 的模型,我保留了这些级别的版本。每个级别都有一个 from_date 和与之相关的成本。每当有人更改日期时,都会创建新版本。
我允许人们根据需要删除旧版本,这很有效。我希望能够修改旧 paper_trail 版本并在不创建新版本的情况下保存它。
class Level < ActiveRecord::Base
has_paper_trail :only => [:from_date],
:if => Proc.new { |l|
l.versions.count == 0 || l.versions.first.item != nil && (l.versions.first.item.from_date.nil? || l.from_date > l.versions.first.item.from_date)
}
<snip code>
end
如果我执行以下操作,它只会更新当前级别而不是版本
level = Level.find 1
version=level.versions[1].reify
version.cost_cents = 1000
version.save
是否有更新旧版本的 cost_cents?
还有没有办法在不创建新版本的情况下更新旧版本的 from_date?
Is there anyway to update the cost_cents for an old version?
是的,但我唯一知道的方法有点尴尬。
PaperTrail::Version
是一个普通的 ActiveRecord
对象,所以从这个意义上讲它很容易使用,但是数据是序列化的(在 YAML 中,默认情况下)所以你必须去 -序列化,进行更改,然后重新序列化。
v = PaperTrail::Version.last
hash = YAML.load(v.object)
hash[:my_attribute] = "my new value"
v.object = YAML.dump(hash)
v.save
使用 ActiveRecord 的自动序列化功能(如 ActiveRecord::AttributeMethods::Serialization
)可能有更好的方法来执行此操作。
PS:我看到您正在尝试使用 reify
,其中 returns 是您模型的实例,而不是 PaperTrail::Version
.[=17= 的实例]