Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
yaml 356 bytes, json 346 bytes
# with YAML
# ------------
irb(main):005:0> Widget.create(name: "asldkfj", a_text: "lsdkjflskdjf", an_integer: 11341, a_float: 1.3214, a_decimal: 14.31424, a_datetime: DateTime.new(), a_time: Time.current, a_date: Date.new(2016, 4, 11))
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "widgets" ("name", "a_text", "an_integer", "a_float", "a_decimal", "a_datetime", "a_time", "a_date", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["name", "asldkfj"], ["a_text", "lsdkjflskdjf"], ["an_integer", 11341], ["a_float", 1.3214], ["a_decimal", 14.3142], ["a_datetime", "-4712-01-01 00:00:00.000000"], ["a_time", "2016-04-11 05:25:31.453950"], ["a_date", "2016-04-11"], ["created_at", "2016-04-11 05:25:31.465095"], ["updated_at", "2016-04-11 05:25:31.465095"]]
SQL (0.2ms) INSERT INTO "versions" ("event", "created_at", "object_changes", "item_id", "item_type") VALUES (?, ?, ?, ?, ?) [["event", "create"], ["created_at", "2016-04-11 05:25:31.465095"], ["object_changes", "---\nname:\n- \n- asldkfj\na_text:\n- \n- lsdkjflskdjf\nan_integer:\n- \n- 11341\na_float:\n- \n- 1.3214\na_decimal:\n- \n- !ruby/object:BigDecimal 27:0.143142E2\na_datetime:\n- \n- !ruby/object:DateTime -4712-01-01 00:00:00.000000000 Z\na_time:\n- \n- 2016-04-11 05:25:31.453950000 Z\na_date:\n- \n- 2016-04-11\ncreated_at:\n- \n- &1 2016-04-11 05:25:31.465095000 Z\nupdated_at:\n- \n- *1\nid:\n- \n- 1\n"], ["item_id", 1], ["item_type", "Widget"]]
SQL (0.4ms) UPDATE "versions" SET "transaction_id" = ? WHERE "versions"."id" = ? [["transaction_id", 1], ["id", 1]]
(5.6ms) commit transaction
=> #<Widget id: 1, name: "asldkfj", a_text: "lsdkjflskdjf", an_integer: 11341, a_float: 1.3214, a_decimal: #<BigDecimal:7f8a7be38cf0,'0.143142E2',18(27)>, a_datetime: "-4712-01-01 00:00:00", a_time: "2016-04-11 05:25:31", a_date: "2016-04-11", a_boolean: nil, sacrificial_column: nil, type: nil, created_at: "2016-04-11 05:25:31", updated_at: "2016-04-11 05:25:31">
irb(main):006:0> w = _ => #<Widget id: 1, name: "asldkfj", a_text: "lsdkjflskdjf", an_integer: 11341, a_float: 1.3214, a_decimal: #<BigDecimal:7f8a7be38cf0,'0.143142E2',18(27)>, a_datetime: "-4712-01-01 00:00:00", a_time: "2016-04-11 05:25:31", a_date: "2016-04-11", a_boolean: nil, sacrificial_column: nil, type: nil, created_at: "2016-04-11 05:25:31", updated_at: "2016-04-11 05:25:31">
irb(main):007:0> w.update(name: "ksdjflsdkf")
(0.1ms) begin transaction
SQL (0.5ms) UPDATE "widgets" SET "name" = ?, "updated_at" = ? WHERE "widgets"."id" = ? [["name", "ksdjflsdkf"], ["updated_at", "2016-04-11 05:25:54.621703"], ["id", 1]]
SQL (0.2ms) INSERT INTO "versions" ("event", "object", "created_at", "object_changes", "item_id", "item_type") VALUES (?, ?, ?, ?, ?, ?) [["event", "update"], ["object", "---\nid: 1\nname: asldkfj\na_text: lsdkjflskdjf\nan_integer: 11341\na_float: 1.3214\na_decimal: !ruby/object:BigDecimal 27:0.143142E2\na_datetime: !ruby/object:DateTime -4712-01-01 00:00:00.000000000 Z\na_time: 2016-04-11 05:25:31.453950000 Z\na_date: 2016-04-11\na_boolean: \nsacrificial_column: \ntype: \ncreated_at: &1 2016-04-11 05:25:31.465095000 Z\nupdated_at: *1\n"], ["created_at", "2016-04-11 05:25:54.621703"], ["object_changes", "---\nname:\n- asldkfj\n- ksdjflsdkf\nupdated_at:\n- 2016-04-11 05:25:31.465095000 Z\n- 2016-04-11 05:25:54.621703000 Z\n"], ["item_id", 1], ["item_type", "Widget"]]
SQL (0.5ms) UPDATE "versions" SET "transaction_id" = ? WHERE "versions"."id" = ? [["transaction_id", 2], ["id", 2]]
(3.3ms) commit transaction
=> true
irb(main):008:0> w.versions.last.object
PaperTrail::Version Load (0.4ms) SELECT "versions".* FROM "versions" WHERE "versions"."item_id" = ? AND "versions"."item_type" = ? ORDER BY "versions"."created_at" DESC, "versions"."id" DESC LIMIT 1 [["item_id", 1], ["item_type", "Widget"]]
=> "---\nid: 1\nname: asldkfj\na_text: lsdkjflskdjf\nan_integer: 11341\na_float: 1.3214\na_decimal: !ruby/object:BigDecimal 27:0.143142E2\na_datetime: !ruby/object:DateTime -4712-01-01 00:00:00.000000000 Z\na_time: 2016-04-11 05:25:31.453950000 Z\na_date: 2016-04-11\na_boolean: \nsacrificial_column: \ntype: \ncreated_at: &1 2016-04-11 05:25:31.465095000 Z\nupdated_at: *1\n"
irb(main):009:0> w.versions.last.object.length
PaperTrail::Version Load (0.3ms) SELECT "versions".* FROM "versions" WHERE "versions"."item_id" = ? AND "versions"."item_type" = ? ORDER BY "versions"."created_at" DESC, "versions"."id" DESC LIMIT 1 [["item_id", 1], ["item_type", "Widget"]]
=> 356
# now with JSON
# ------
irb(main):010:0> ^D
01:26:45 daffodil dummy (require_explicit_serializer)$ RAILS_ENV=test be rails c
Loading test environment (Rails 4.2.6)
irb(main):001:0> Widget.create(name: "asldkfj", a_text: "lsdkjflskdjf", an_integer: 11341, a_float: 1.3214, a_decimal: 14.31424, a_datetime: DateTime.new(), a_time: Time.current, a_date: Date.new(2016, 4, 11))
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "widgets" ("name", "a_text", "an_integer", "a_float", "a_decimal", "a_datetime", "a_time", "a_date", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["name", "asldkfj"], ["a_text", "lsdkjflskdjf"], ["an_integer", 11341], ["a_float", 1.3214], ["a_decimal", 14.3142], ["a_datetime", "-4712-01-01 00:00:00.000000"], ["a_time", "2016-04-11 05:27:12.092464"], ["a_date", "2016-04-11"], ["created_at", "2016-04-11 05:27:12.106503"], ["updated_at", "2016-04-11 05:27:12.106503"]]
SQL (0.2ms) INSERT INTO "versions" ("event", "created_at", "object_changes", "item_id", "item_type") VALUES (?, ?, ?, ?, ?) [["event", "create"], ["created_at", "2016-04-11 05:27:12.106503"], ["object_changes", "{\"name\":[null,\"asldkfj\"],\"a_text\":[null,\"lsdkjflskdjf\"],\"an_integer\":[null,11341],\"a_float\":[null,1.3214],\"a_decimal\":[null,\"14.3142\"],\"a_datetime\":[null,\"-4712-01-01T00:00:00.000+00:00\"],\"a_time\":[null,\"2016-04-11T05:27:12.092Z\"],\"a_date\":[null,\"2016-04-11\"],\"created_at\":[null,\"2016-04-11T05:27:12.106Z\"],\"updated_at\":[null,\"2016-04-11T05:27:12.106Z\"],\"id\":[null,2]}"], ["item_id", 2], ["item_type", "Widget"]]
SQL (0.5ms) UPDATE "versions" SET "transaction_id" = ? WHERE "versions"."id" = ? [["transaction_id", 3], ["id", 3]]
(4.0ms) commit transaction
=> #<Widget id: 2, name: "asldkfj", a_text: "lsdkjflskdjf", an_integer: 11341, a_float: 1.3214, a_decimal: #<BigDecimal:7fb26d2b8ac8,'0.143142E2',18(27)>, a_datetime: "-4712-01-01 00:00:00", a_time: "2016-04-11 05:27:12", a_date: "2016-04-11", a_boolean: nil, sacrificial_column: nil, type: nil, created_at: "2016-04-11 05:27:12", updated_at: "2016-04-11 05:27:12">
irb(main):002:0> w = _
=> #<Widget id: 2, name: "asldkfj", a_text: "lsdkjflskdjf", an_integer: 11341, a_float: 1.3214, a_decimal: #<BigDecimal:7fb26d2b8ac8,'0.143142E2',18(27)>, a_datetime: "-4712-01-01 00:00:00", a_time: "2016-04-11 05:27:12", a_date: "2016-04-11", a_boolean: nil, sacrificial_column: nil, type: nil, created_at: "2016-04-11 05:27:12", updated_at: "2016-04-11 05:27:12">
irb(main):003:0> w.update(name: "ksdjflsdkf")
(0.1ms) begin transaction
SQL (0.5ms) UPDATE "widgets" SET "name" = ?, "updated_at" = ? WHERE "widgets"."id" = ? [["name", "ksdjflsdkf"], ["updated_at", "2016-04-11 05:27:29.892067"], ["id", 2]]
SQL (0.2ms) INSERT INTO "versions" ("event", "object", "created_at", "object_changes", "item_id", "item_type") VALUES (?, ?, ?, ?, ?, ?) [["event", "update"], ["object", "{\"id\":2,\"name\":\"asldkfj\",\"a_text\":\"lsdkjflskdjf\",\"an_integer\":11341,\"a_float\":1.3214,\"a_decimal\":\"14.3142\",\"a_datetime\":\"-4712-01-01T00:00:00.000+00:00\",\"a_time\":\"2016-04-11T05:27:12.092Z\",\"a_date\":\"2016-04-11\",\"a_boolean\":null,\"sacrificial_column\":null,\"type\":null,\"created_at\":\"2016-04-11T05:27:12.106Z\",\"updated_at\":\"2016-04-11T05:27:12.106Z\"}"], ["created_at", "2016-04-11 05:27:29.892067"], ["object_changes", "{\"name\":[\"asldkfj\",\"ksdjflsdkf\"],\"updated_at\":[\"2016-04-11T05:27:12.106Z\",\"2016-04-11T05:27:29.892Z\"]}"], ["item_id", 2], ["item_type", "Widget"]]
SQL (0.3ms) UPDATE "versions" SET "transaction_id" = ? WHERE "versions"."id" = ? [["transaction_id", 4], ["id", 4]]
(2.6ms) commit transaction
=> true
irb(main):004:0> w.versions.last.object.length
PaperTrail::Version Load (0.4ms) SELECT "versions".* FROM "versions" WHERE "versions"."item_id" = ? AND "versions"."item_type" = ? ORDER BY "versions"."created_at" DESC, "versions"."id" DESC LIMIT 1 [["item_id", 2], ["item_type", "Widget"]]
=> 346
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment