Advertisement
Guest User

Untitled

a guest
May 12th, 2017
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.28 KB | None | 0 0
  1. # belongs_to side contains the foreign key
  2. class Comment < ActiveRecord::Base
  3. belongs_to :example
  4. end
  5.  
  6. # has_one side does not contain the foreign key
  7. class Example < ApplicationRecord
  8. belongs_to :user
  9. has_one :comment
  10. validates :text, :user, presence: true
  11. end
  12.  
  13. # Below is creating and testing via the rails console:
  14.  
  15. dkirschner@Dannys-Island-Paradise.local: rails-api-bdd (training-017)* $ rails g migration addExampleIdToComment example:references
  16. Running via Spring preloader in process 66200
  17. Expected string default value for '--serializer'; got true (boolean)
  18. invoke active_record
  19. create db/migrate/20170512151033_add_example_id_to_comment.rb
  20. dkirschner@Dannys-Island-Paradise.local: rails-api-bdd (training-017)* $ vim .
  21. dkirschner@Dannys-Island-Paradise.local: rails-api-bdd (training-017)* $ rails db:migrate
  22. == 20170512151033 AddExampleIdToComment: migrating ============================
  23. -- add_reference(:comments, :example, {:foreign_key=>true})
  24. -> 0.0877s
  25. == 20170512151033 AddExampleIdToComment: migrated (0.0878s) ===================
  26.  
  27. dkirschner@Dannys-Island-Paradise.local: rails-api-bdd (training-017)* $ rails c
  28. Running via Spring preloader in process 66670
  29. Loading development environment (Rails 5.0.1)
  30. [1] pry(main)> comment = Comment.new
  31. => #<Comment:0x007fcf0d4513c8 id: nil, body: nil, created_at: nil, updated_at: nil, article_id: nil, example_id: nil>
  32. [2] pry(main)> comment.example.build
  33. NoMethodError: undefined method `build' for nil:NilClass
  34. from (pry):2:in `<main>'
  35. [3] pry(main)> comment.build_example
  36. => #<Example:0x007fcf0a9f6e70 id: nil, text: nil, user_id: nil, created_at: nil, updated_at: nil>
  37. [4] pry(main)> comment.example.nil?
  38. => false
  39. [5] pry(main)> comment.example
  40. => #<Example:0x007fcf0a9f6e70 id: nil, text: nil, user_id: nil, created_at: nil, updated_at: nil>
  41. [6] pry(main)> comment.save
  42. (0.2ms) BEGIN
  43. SQL (18.7ms) INSERT INTO "comments" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", 2017-05-12 15:21:00 UTC], ["updated_at", 2017-05-12 15:21:00 UTC]]
  44. (0.7ms) COMMIT
  45. => true
  46. [7] pry(main)> comment
  47. => #<Comment:0x007fcf0d4513c8
  48. id: 1,
  49. body: nil,
  50. created_at: Fri, 12 May 2017 15:21:00 UTC +00:00,
  51. updated_at: Fri, 12 May 2017 15:21:00 UTC +00:00,
  52. article_id: nil,
  53. example_id: nil>
  54. [8] pry(main)> comment.example
  55. => #<Example:0x007fcf0a9f6e70 id: nil, text: nil, user_id: nil, created_at: nil, updated_at: nil>
  56. [9] pry(main)> comment.example.valid?
  57. => false
  58. [10] pry(main)> comment.example.errors
  59. => #<ActiveModel::Errors:0x007fcf0cb9b2b0
  60. @base=#<Example:0x007fcf0a9f6e70 id: nil, text: nil, user_id: nil, created_at: nil, updated_at: nil>,
  61. @details={:user=>[{:error=>:blank}, {:error=>:blank}], :text=>[{:error=>:blank}]},
  62. @messages={:user=>["must exist", "can't be blank"], :text=>["can't be blank"]}>
  63. [11] pry(main)> comment.example.user
  64. => nil
  65. [12] pry(main)> comment.example.user = User.last
  66. User Load (0.6ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT $1 [["LIMIT", 1]]
  67. => nil
  68. [13] pry(main)> User.create
  69. (0.2ms) BEGIN
  70. User Exists (0.5ms) SELECT 1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT $1 [["LIMIT", 1]]
  71. (5.4ms) ROLLBACK
  72. => #<User:0x007fcf0d002ad8 id: nil, email: nil, token: nil, password_digest: nil, created_at: nil, updated_at: nil>
  73. [14] pry(main)> User.create(email: 'd@k')
  74. (0.2ms) BEGIN
  75. User Exists (0.7ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "d@k"], ["LIMIT", 1]]
  76. (0.2ms) ROLLBACK
  77. => #<User:0x007fcf09f138a0 id: nil, email: "d@k", token: nil, password_digest: nil, created_at: nil, updated_at: nil>
  78. [15] pry(main)> User.create(email: 'd@k', password: 'q')
  79. (0.2ms) BEGIN
  80. User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "d@k"], ["LIMIT", 1]]
  81. SQL (8.7ms) INSERT INTO "users" ("email", "token", "password_digest", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["email", "d@k"], ["token", "939a704cb70210a3386d013336c109b5"], ["password_digest", "$2a$10$h8nFhz43zbBskZz3n1HSrujOB9N64hOyuvdkghouegij0DkSP1Cvy"], ["created_at", 2017-05-12 15:21:40 UTC], ["updated_at", 2017-05-12 15:21:40 UTC]]
  82. (0.5ms) COMMIT
  83. => #<User:0x007fcf0c8e3f18
  84. id: 1,
  85. email: "d@k",
  86. token: "939a704cb70210a3386d013336c109b5",
  87. password_digest: "$2a$10$h8nFhz43zbBskZz3n1HSrujOB9N64hOyuvdkghouegij0DkSP1Cvy",
  88. created_at: Fri, 12 May 2017 15:21:40 UTC +00:00,
  89. updated_at: Fri, 12 May 2017 15:21:40 UTC +00:00>
  90. [16] pry(main)> comment.example.user = User.last
  91. User Load (0.4ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT $1 [["LIMIT", 1]]
  92. => #<User:0x007fcf09e6ae80
  93. id: 1,
  94. email: "d@k",
  95. token: "939a704cb70210a3386d013336c109b5",
  96. password_digest: "$2a$10$h8nFhz43zbBskZz3n1HSrujOB9N64hOyuvdkghouegij0DkSP1Cvy",
  97. created_at: Fri, 12 May 2017 15:21:40 UTC +00:00,
  98. updated_at: Fri, 12 May 2017 15:21:40 UTC +00:00>
  99. [17] pry(main)> comment.example.valid?
  100. => false
  101. [18] pry(main)> comment.example.errors
  102. => #<ActiveModel::Errors:0x007fcf0cb9b2b0
  103. @base=#<Example:0x007fcf0a9f6e70 id: nil, text: nil, user_id: 1, created_at: nil, updated_at: nil>,
  104. @details={:text=>[{:error=>:blank}]},
  105. @messages={:text=>["can't be blank"]}>
  106. [19] pry(main)> comment.example.text = "blah"
  107. => "blah"
  108. [20] pry(main)> comment.example.valid?
  109. => true
  110. [21] pry(main)> comment.example.save
  111. (0.2ms) BEGIN
  112. SQL (9.7ms) INSERT INTO "examples" ("text", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["text", "blah"], ["user_id", 1], ["created_at", 2017-05-12 15:22:03 UTC], ["updated_at", 2017-05-12 15:22:03 UTC]]
  113. Example Load (0.3ms) SELECT "examples".* FROM "examples" WHERE "examples"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
  114. SQL (0.7ms) UPDATE "comments" SET "updated_at" = $1, "example_id" = $2 WHERE "comments"."id" = $3 [["updated_at", 2017-05-12 15:22:03 UTC], ["example_id", 1], ["id", 1]]
  115. (5.7ms) COMMIT
  116. => true
  117. [22] pry(main)> comment.exapmle
  118. NoMethodError: undefined method `exapmle' for #<Comment:0x007fcf0d4513c8>
  119. Did you mean? example
  120. from /Users/dkirschner/.rvm/gems/ruby-2.3.1/gems/activemodel-5.0.1/lib/active_model/attribute_methods.rb:433:in `method_missing'
  121. [23] pry(main)> comment.example
  122. => #<Example:0x007fcf09e40180
  123. id: 1,
  124. text: "blah",
  125. user_id: 1,
  126. created_at: Fri, 12 May 2017 15:22:03 UTC +00:00,
  127. updated_at: Fri, 12 May 2017 15:22:03 UTC +00:00>
  128. [24] pry(main)> comment.example.nil?
  129. => false
  130. [25] pry(main)> Example.last
  131. Example Load (0.3ms) SELECT "examples".* FROM "examples" ORDER BY "examples"."id" DESC LIMIT $1 [["LIMIT", 1]]
  132. => #<Example:0x007fcf0a5b07f8
  133. id: 1,
  134. text: "blah",
  135. user_id: 1,
  136. created_at: Fri, 12 May 2017 15:22:03 UTC +00:00,
  137. updated_at: Fri, 12 May 2017 15:22:03 UTC +00:00>
  138. [26] pry(main)> Example.last.comment
  139. Example Load (0.4ms) SELECT "examples".* FROM "examples" ORDER BY "examples"."id" DESC LIMIT $1 [["LIMIT", 1]]
  140. Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."example_id" = $1 LIMIT $2 [["example_id", 1], ["LIMIT", 1]]
  141. => #<Comment:0x007fcf09cd2398
  142. id: 1,
  143. body: nil,
  144. created_at: Fri, 12 May 2017 15:21:00 UTC +00:00,
  145. updated_at: Fri, 12 May 2017 15:22:03 UTC +00:00,
  146. article_id: nil,
  147. example_id: 1>
  148. [27] pry(main)> Example.last.comments
  149. Example Load (0.5ms) SELECT "examples".* FROM "examples" ORDER BY "examples"."id" DESC LIMIT $1 [["LIMIT", 1]]
  150. NoMethodError: undefined method `comments' for #<Example:0x007fcf09c7b7f0>
  151. Did you mean? comment
  152. comment=
  153. from /Users/dkirschner/.rvm/gems/ruby-2.3.1/gems/activemodel-5.0.1/lib/active_model/attribute_methods.rb:433:in `method_missing'
  154. [28] pry(main)> Example.last.comment
  155. Example Load (0.4ms) SELECT "examples".* FROM "examples" ORDER BY "examples"."id" DESC LIMIT $1 [["LIMIT", 1]]
  156. Comment Load (0.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."example_id" = $1 LIMIT $2 [["example_id", 1], ["LIMIT", 1]]
  157. => #<Comment:0x007fcf0a4da978
  158. id: 1,
  159. body: nil,
  160. created_at: Fri, 12 May 2017 15:21:00 UTC +00:00,
  161. updated_at: Fri, 12 May 2017 15:22:03 UTC +00:00,
  162. article_id: nil,
  163. example_id: 1>
  164. [29] pry(main)> Example.last.comment.nil?
  165. Example Load (0.3ms) SELECT "examples".* FROM "examples" ORDER BY "examples"."id" DESC LIMIT $1 [["LIMIT", 1]]
  166. Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."example_id" = $1 LIMIT $2 [["example_id", 1], ["LIMIT", 1]]
  167. => false
  168. [30] pry(main)>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement