Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. class Post < ApplicationRecord
  2. after_save :save_post_copy
  3.  
  4. private
  5.  
  6. def save_post_copy
  7. if will_save_change_to_title? == nil and saved_change_to_title?
  8. post_copy = PostCopy.find_by(id: self.id)
  9. if post_copy.nil?
  10. PostCopy.create!(id: self.id, title: self.title, body: self.body)
  11. else
  12. post_copy.update!(title: self.title, body: self.body)
  13. end
  14. end
  15. end
  16. end
  17.  
  18. post = Post.new
  19. => #<Post id: nil, title: nil, body: nil, created_at: nil, updated_at: nil>
  20. post.body = 'test'
  21. => "test"
  22. post.save!
  23. (0.1ms) begin transaction
  24. Post Create (0.9ms) INSERT INTO "posts" ("body", "created_at", "updated_at") VALUES (?, ?, ?) [["body", "test"], ["created_at", "2019-05-20 07:19:50.289983"], ["updated_at", "2019-05-20 07:19:50.289983"]]
  25. (6.5ms) commit transaction
  26. => true
  27. post.title = '3'
  28. => '3'
  29. post.save!
  30. (0.1ms) begin transaction
  31. Post Update (0.5ms) UPDATE "posts" SET "title" = ?, "updated_at" = ? WHERE "posts"."id" = ? [["title", "3"], ["updated_at", "2019-05-20 07:20:04.138089"], ["id", 8]]
  32. PostCopy Load (0.1ms) SELECT "post_copies".* FROM "post_copies" WHERE "post_copies"."id" = ? LIMIT ? [["id", 8], ["LIMIT", 1]]
  33. PostCopy Create (0.3ms) INSERT INTO "post_copies" ("title", "body", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["title", "3"], ["body", "test"], ["created_at", "2019-05-20 07:20:04.148852"], ["updated_at", "2019-05-20 07:20:04.
  34. 148852"]]
  35. (7.1ms) commit transaction
  36. => true
  37.  
  38. post
  39. => #<Post id: 10, title: "ten", body: "10", created_at: "2019-05-20 07:21:08", updated_at: "2019-05-20 07:21:41">
  40. post.title = nil
  41. post.save!
  42. (0.1ms) begin transaction
  43. Post Update (1.4ms) UPDATE "posts" SET "title" = ?, "updated_at" = ? WHERE "posts"."id" = ? [["title", nil], ["updated_at", "2019-05-20 07:34:15.594000"], ["id", 10]]
  44. PostCopy Load (0.1ms) SELECT "post_copies".* FROM "post_copies" WHERE "post_copies"."id" = ? LIMIT ? [["id", 10], ["LIMIT", 1]]
  45. PostCopy Create (0.3ms) INSERT INTO "post_copies" ("body", "created_at", "updated_at") VALUES (?, ?, ?) [["body", "10"], ["created_at", "2019-05-20 07:34:15.598453"], ["updated_at", "2019-05-20 07:34:15.598453"]]
  46. (1.8ms) commit transaction
  47. => true
  48.  
  49. post
  50. => #<Post id: 10, title: "ten", body: "10", created_at: "2019-05-20 07:21:08", updated_at: "2019-05-20 07:21:41">
  51. post.will_save_change_to_title?
  52. => nil
  53. post.title = '1'
  54. => "1"
  55. post.will_save_change_to_title?
  56. => true
  57. post.save!
  58. (0.1ms) begin transaction
  59. Post Update (0.6ms) UPDATE "posts" SET "title" = ?, "updated_at" = ? WHERE "posts"."id" = ? [["title", "1"], ["updated_at", "2019-05-20 07:37:46.467899"], ["id", 10]]
  60. PostCopy Load (0.1ms) SELECT "post_copies".* FROM "post_copies" WHERE "post_copies"."id" = ? LIMIT ? [["id", 10], ["LIMIT", 1]]
  61. PostCopy Update (0.3ms) UPDATE "post_copies" SET "title" = ?, "updated_at" = ? WHERE "post_copies"."id" = ? [["title", "1"], ["updated_at", "2019-05-20 07:37:46.471654"], ["id", 10]]
  62. (1.3ms) commit transaction
  63. => true
  64.  
  65. person = Person.new
  66. person.changed? # => false
  67. person.name = 'Bob'
  68. person.changed? # => true
  69. person.name_changed? # => true
  70. person.name_changed?(from: nil, to: "Bob") # => true
  71. person.name_was # => nil
  72. person.name_change # => [nil, "Bob"]
  73. person.name = 'Bill'
  74. person.name_change # => [nil, "Bill"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement