I’m starting to use Paper Trail gem, I have this Product model:
- name:string qty:integer color:string
and inside my product.rb I have this:
- has_paper_trail
So everything works fine, I mave 2 updates to a single product only modifying the qty.
In my rails console I did the following:
Product.last.versions.map(&:object)
And got this
[“—\nname: p1\nqty: 20\ncolor: c1\ncreated_at: 2012-03-14 03:02:22.186453000 Z\nupdated_at: 2012-03-14 03:02:22.186453000 Z\nid: 3\n”, “—\nname: p1\nqty: 40\ncolor: c1\ncreated_at: 2012-03-14 03:02:22.186453000 Z\nupdated_at: 2012-03-14 03:07:53.291624000 Z\nid: 3\n”]
and when I do this:
puts Product.last.versions.map(&:object)
I got this:
---
name: p1
qty: 20
color: c1
created_at: 2012-03-14 03:02:22.186453000 Z
updated_at: 2012-03-14 03:02:22.186453000 Z
id: 3
---
name: p1
qty: 40
color: c1
created_at: 2012-03-14 03:02:22.186453000 Z
updated_at: 2012-03-14 03:07:53.291624000 Z
id: 3
I was wondering how to obtain the qty of each change. I though of regex but I’m not sure how.
I want to show this in my view
-
Product:
-
Name: c1 Qty: 980
-
Changes:
- (date1) : 20 | (date2) : 40 | …….. | (dateN) : N
-
Any suggestions?
thanks in advance
Its serialized into YAML. There is one way to do it. I’m using the object as
product_versionsyou got from the versions map.This will give you