Skip to content

Instantly share code, notes, and snippets.

@danman01
Last active May 12, 2017 15:28
Show Gist options
  • Save danman01/6284ffd6b32b42157fbbba1b95858c16 to your computer and use it in GitHub Desktop.
Save danman01/6284ffd6b32b42157fbbba1b95858c16 to your computer and use it in GitHub Desktop.
verifying rails associations exist
# belongs_to side contains the foreign key
class Comment < ActiveRecord::Base
belongs_to :example
end
# has_one side does not contain the foreign key
class Example < ApplicationRecord
belongs_to :user
has_one :comment
validates :text, :user, presence: true
end
# Below is creating and testing via the rails console:
[email protected]: rails-api-bdd (training-017)* $ rails g migration addExampleIdToComment example:references
Running via Spring preloader in process 66200
Expected string default value for '--serializer'; got true (boolean)
invoke active_record
create db/migrate/20170512151033_add_example_id_to_comment.rb
[email protected]: rails-api-bdd (training-017)* $ vim .
[email protected]: rails-api-bdd (training-017)* $ rails db:migrate
== 20170512151033 AddExampleIdToComment: migrating ============================
-- add_reference(:comments, :example, {:foreign_key=>true})
-> 0.0877s
== 20170512151033 AddExampleIdToComment: migrated (0.0878s) ===================
[email protected]: rails-api-bdd (training-017)* $ rails c
Running via Spring preloader in process 66670
Loading development environment (Rails 5.0.1)
[1] pry(main)> comment = Comment.new
=> #<Comment:0x007fcf0d4513c8 id: nil, body: nil, created_at: nil, updated_at: nil, article_id: nil, example_id: nil>
[2] pry(main)> comment.example.build
NoMethodError: undefined method `build' for nil:NilClass
from (pry):2:in `<main>'
[3] pry(main)> comment.build_example
=> #<Example:0x007fcf0a9f6e70 id: nil, text: nil, user_id: nil, created_at: nil, updated_at: nil>
[4] pry(main)> comment.example.nil?
=> false
[5] pry(main)> comment.example
=> #<Example:0x007fcf0a9f6e70 id: nil, text: nil, user_id: nil, created_at: nil, updated_at: nil>
[6] pry(main)> comment.save
(0.2ms) BEGIN
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]]
(0.7ms) COMMIT
=> true
[7] pry(main)> comment
=> #<Comment:0x007fcf0d4513c8
id: 1,
body: nil,
created_at: Fri, 12 May 2017 15:21:00 UTC +00:00,
updated_at: Fri, 12 May 2017 15:21:00 UTC +00:00,
article_id: nil,
example_id: nil>
[8] pry(main)> comment.example
=> #<Example:0x007fcf0a9f6e70 id: nil, text: nil, user_id: nil, created_at: nil, updated_at: nil>
[9] pry(main)> comment.example.valid?
=> false
[10] pry(main)> comment.example.errors
=> #<ActiveModel::Errors:0x007fcf0cb9b2b0
@base=#<Example:0x007fcf0a9f6e70 id: nil, text: nil, user_id: nil, created_at: nil, updated_at: nil>,
@details={:user=>[{:error=>:blank}, {:error=>:blank}], :text=>[{:error=>:blank}]},
@messages={:user=>["must exist", "can't be blank"], :text=>["can't be blank"]}>
[11] pry(main)> comment.example.user
=> nil
[12] pry(main)> comment.example.user = User.last
User Load (0.6ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> nil
[13] pry(main)> User.create
(0.2ms) BEGIN
User Exists (0.5ms) SELECT 1 AS one FROM "users" WHERE "users"."email" IS NULL LIMIT $1 [["LIMIT", 1]]
(5.4ms) ROLLBACK
=> #<User:0x007fcf0d002ad8 id: nil, email: nil, token: nil, password_digest: nil, created_at: nil, updated_at: nil>
[14] pry(main)> User.create(email: 'd@k')
(0.2ms) BEGIN
User Exists (0.7ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "d@k"], ["LIMIT", 1]]
(0.2ms) ROLLBACK
=> #<User:0x007fcf09f138a0 id: nil, email: "d@k", token: nil, password_digest: nil, created_at: nil, updated_at: nil>
[15] pry(main)> User.create(email: 'd@k', password: 'q')
(0.2ms) BEGIN
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "d@k"], ["LIMIT", 1]]
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]]
(0.5ms) COMMIT
=> #<User:0x007fcf0c8e3f18
id: 1,
email: "d@k",
token: "939a704cb70210a3386d013336c109b5",
password_digest: "$2a$10$h8nFhz43zbBskZz3n1HSrujOB9N64hOyuvdkghouegij0DkSP1Cvy",
created_at: Fri, 12 May 2017 15:21:40 UTC +00:00,
updated_at: Fri, 12 May 2017 15:21:40 UTC +00:00>
[16] pry(main)> comment.example.user = User.last
User Load (0.4ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<User:0x007fcf09e6ae80
id: 1,
email: "d@k",
token: "939a704cb70210a3386d013336c109b5",
password_digest: "$2a$10$h8nFhz43zbBskZz3n1HSrujOB9N64hOyuvdkghouegij0DkSP1Cvy",
created_at: Fri, 12 May 2017 15:21:40 UTC +00:00,
updated_at: Fri, 12 May 2017 15:21:40 UTC +00:00>
[17] pry(main)> comment.example.valid?
=> false
[18] pry(main)> comment.example.errors
=> #<ActiveModel::Errors:0x007fcf0cb9b2b0
@base=#<Example:0x007fcf0a9f6e70 id: nil, text: nil, user_id: 1, created_at: nil, updated_at: nil>,
@details={:text=>[{:error=>:blank}]},
@messages={:text=>["can't be blank"]}>
[19] pry(main)> comment.example.text = "blah"
=> "blah"
[20] pry(main)> comment.example.valid?
=> true
[21] pry(main)> comment.example.save
(0.2ms) BEGIN
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]]
Example Load (0.3ms) SELECT "examples".* FROM "examples" WHERE "examples"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
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]]
(5.7ms) COMMIT
=> true
[22] pry(main)> comment.exapmle
NoMethodError: undefined method `exapmle' for #<Comment:0x007fcf0d4513c8>
Did you mean? example
from /Users/dkirschner/.rvm/gems/ruby-2.3.1/gems/activemodel-5.0.1/lib/active_model/attribute_methods.rb:433:in `method_missing'
[23] pry(main)> comment.example
=> #<Example:0x007fcf09e40180
id: 1,
text: "blah",
user_id: 1,
created_at: Fri, 12 May 2017 15:22:03 UTC +00:00,
updated_at: Fri, 12 May 2017 15:22:03 UTC +00:00>
[24] pry(main)> comment.example.nil?
=> false
[25] pry(main)> Example.last
Example Load (0.3ms) SELECT "examples".* FROM "examples" ORDER BY "examples"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Example:0x007fcf0a5b07f8
id: 1,
text: "blah",
user_id: 1,
created_at: Fri, 12 May 2017 15:22:03 UTC +00:00,
updated_at: Fri, 12 May 2017 15:22:03 UTC +00:00>
[26] pry(main)> Example.last.comment
Example Load (0.4ms) SELECT "examples".* FROM "examples" ORDER BY "examples"."id" DESC LIMIT $1 [["LIMIT", 1]]
Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."example_id" = $1 LIMIT $2 [["example_id", 1], ["LIMIT", 1]]
=> #<Comment:0x007fcf09cd2398
id: 1,
body: nil,
created_at: Fri, 12 May 2017 15:21:00 UTC +00:00,
updated_at: Fri, 12 May 2017 15:22:03 UTC +00:00,
article_id: nil,
example_id: 1>
[27] pry(main)> Example.last.comments
Example Load (0.5ms) SELECT "examples".* FROM "examples" ORDER BY "examples"."id" DESC LIMIT $1 [["LIMIT", 1]]
NoMethodError: undefined method `comments' for #<Example:0x007fcf09c7b7f0>
Did you mean? comment
comment=
from /Users/dkirschner/.rvm/gems/ruby-2.3.1/gems/activemodel-5.0.1/lib/active_model/attribute_methods.rb:433:in `method_missing'
[28] pry(main)> Example.last.comment
Example Load (0.4ms) SELECT "examples".* FROM "examples" ORDER BY "examples"."id" DESC LIMIT $1 [["LIMIT", 1]]
Comment Load (0.4ms) SELECT "comments".* FROM "comments" WHERE "comments"."example_id" = $1 LIMIT $2 [["example_id", 1], ["LIMIT", 1]]
=> #<Comment:0x007fcf0a4da978
id: 1,
body: nil,
created_at: Fri, 12 May 2017 15:21:00 UTC +00:00,
updated_at: Fri, 12 May 2017 15:22:03 UTC +00:00,
article_id: nil,
example_id: 1>
[29] pry(main)> Example.last.comment.nil?
Example Load (0.3ms) SELECT "examples".* FROM "examples" ORDER BY "examples"."id" DESC LIMIT $1 [["LIMIT", 1]]
Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."example_id" = $1 LIMIT $2 [["example_id", 1], ["LIMIT", 1]]
=> false
[30] pry(main)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment