[英]Edit a paper_trail version without creating a new version
I'm using paper_trail 3.0.8 on a Rails 3.2 app and I've got a model called 'levels' and I keep versions of these levels. 我在Rails 3.2应用程序上使用paper_trail 3.0.8,并且有一个名为“ levels”的模型,并且保留了这些级别的版本。 Each level has a from_date and a cost relating to it.
每个级别都有一个起始日期和与其相关的成本。 Whenever someone changes the date a new version is created.
每当有人更改日期时,都会创建一个新版本。
I allow people to remove old versions if they want and this works well. 我允许人们删除旧版本,并且效果很好。 I would like the ability to modify an old paper_trail version and save it without creating a new version.
我希望能够修改旧的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
If I do the following it only updates the current level and not the version 如果我执行以下操作,它将仅更新当前级别,而不更新版本
level = Level.find 1
version=level.versions[1].reify
version.cost_cents = 1000
version.save
Is there anyway to update the cost_cents for an old version? 无论如何,有没有为旧版本更新cost_cents?
Also is there a way to update the from_date of an old version without creating a new version on the save? 还有没有一种方法可以更新旧版本的from_date而不在保存时创建新版本?
Is there anyway to update the cost_cents for an old version?
无论如何,有没有为旧版本更新cost_cents?
Yes, but the only way I know is a bit awkward. 是的,但是我知道的唯一方法是有点尴尬。
PaperTrail::Version
is a normal ActiveRecord
object, so it's easy to work with in that sense, but the data is serialized (in YAML, by default) so you'll have to de-serialize, make your change, and re-serialize. 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
There may be a better way to do this with ActiveRecord's automatic-serialization features (like ActiveRecord::AttributeMethods::Serialization
). 使用ActiveRecord的自动序列化功能(例如
ActiveRecord::AttributeMethods::Serialization
),可能会有更好的方法。
PS: I see you were trying to use reify
, which returns an instance of your model, not an instance of PaperTrail::Version
. PS:我看到您正在尝试使用
reify
,该方法返回模型的实例,而不是PaperTrail::Version
的实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.