Skip to content

Instantly share code, notes, and snippets.

@Domon
Created May 26, 2012 02:22
Show Gist options
  • Select an option

  • Save Domon/2791759 to your computer and use it in GitHub Desktop.

Select an option

Save Domon/2791759 to your computer and use it in GitHub Desktop.
When we compare two Active Record objects, we are comparing their ids.
[1] pry(main)> p1 = Post.first
Post Load (0.2ms) SELECT "posts".* FROM "posts" LIMIT 1
=> #<Post id: 4, title: "Hello", content: "World.", created_at: "2012-05-20 08:38:46", updated_at: "2012-05-20 08:38:46">
[2] pry(main)> p2 = Post.first
Post Load (0.2ms) SELECT "posts".* FROM "posts" LIMIT 1
=> #<Post id: 4, title: "Hello", content: "World.", created_at: "2012-05-20 08:38:46", updated_at: "2012-05-20 08:38:46">
[3] pry(main)> p1 == p2
=> true
[4] pry(main)> p1.title = "hi"
=> "hi"
[5] pry(main)> p1 == p2
=> true
[6] pry(main)> p1.hash == p2.hash
=> true
[7] pry(main)> p1.hash
=> -2154204912980276923
[8] pry(main)> p1.id.hash
=> -2154204912980276923
@loveybot
Copy link

Yay Ruby!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment