{"action":"created","comment":{"url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/pulls/comments/45184435","id":45184435,"diff_hunk":"@@ -1,3 +1,127 @@\n-# orm-find-or-create-by\n+# ORM: Preventing Record Duplication\n \n-now that we have a way to save records and update records, what happens when we create a new ruby object that reflects a record that is already persisted to our database? In this case we want to update the original record. So, find_or_create_by\n+## Objectives\n+\n+1. Learn how to avoid creating duplicate records in a database that is mapped to a Ruby program. \n+2. Build a `#find_or_create_by` method\n+\n+## The Dreaded Duplication\n+\n+What happens when two Ruby objects get created using the same attributes? If we are trying to persist representations of such objects to a database, would we end up with essentially identical rows in our table? That would make for a very confusing database and our program would quickly become useless as a way to store and manage information. \n+\n+For example, lets say we have a `Song` class that produces individual song objects, each of which have a `name` and `album` attribute.\n+\n+Nothing stops us from creating two objects, each of which have the exact same name and album. \n+\n+```ruby\n+hello = Song.new(\"Hello\", \"25\")\n+hello_again = Song.new(\"Hello\", \"25\")\n+```\n+\n+What happens when we save these objects to our database?\n+\n+*For this example, we'll assume our connection to the database is stored in `DB[:conn]`*\n+\n+```ruby\n+hello.save\n+hello_again.save\n+\n+DB[:conn].execute(\"SELECT * FROM songs WHERE name = \"Hello\")\n+# => [[1, \"Hello\", \"25\"], [2, \"Hello\", \"25\"]]\n+```\n+\n+We have two records that contain the same information! How can we avoid this? When we try to save a new `Song` instance, we should first check to see the object we are trying to save already has an equivalent record in the database, if it does, we should simply update it, otherwise, we can go ahead and save it. \n+\n+## Saving vs. Updating\n+\n+Let's say we have a song, `hello`:\n+\n+```ruby\n+hello = Song.new(\"Hello\", \"25\")\n+```\n+\n+Before we call `#save` on our `hello` object, we need to check and see if a record containing this name and album already exists in the database. The SQL statement to accomplish that would look something like this:\n+\n+```ruby\n+SELECT * FROM songs\n+WHERE name = \"Hello\", album = \"25\";\n+```\n+\n+If this statement returns a record, we don't need to create a new record, only update the existing one. Otherwise, we need to insert a new record into our database table. \n+\n+Let's build a method that will allow us to either *find and update* and existing record or *create and save* and new one. \n+\n+## The `#find_or_create_by` Method\n+\n+Take a look at our `Song` class. \n+\n+```ruby\n+class Song\n+\n+attr_accessor :name, :album\n+attr_reader :id\n+  \n+  def initialize(id=nil, name, album)\n+    @id = id\n+    @name = name\n+    @album = album\n+  end\n+\n+  def save\n+    sql = <<-SQL\n+      INSERT INTO songs (name, album) \n+      VALUES (?, ?)\n+    SQL\n+\n+    DB[:conn].execute(sql, self.name, self.album)\n+    \n+  end\n+\n+  def self.create(name:, album:)\n+    student = Student.new(name, album)\n+    student.save\n+  end\n+  \n+  def self.find_by_name(name)\n+    sql = \"SELECT * FROM songs WHERE name = ?\"\n+    result = DB[:conn].execute(sql, name)[0]\n+    Song.new(result[0], result[1], result[2])\n+  end\n+  \n+  def update\n+    sql = \"UPDATE songs SET name = ?, album = ? WHERE id = ?\"\n+    DB[:conn].execute(sql, self.name, self.album, self.id)\n+  end\n+end\n+``` \n+\n+Let's build our `#find_or_create_by` method:\n+\n+```ruby\n+  def self.find_or_create_by(name:, album:)\n+    if !DB[:conn].execute(\"SELECT * FROM students WHERE name = #{name}, album = #{album}\").empty?\n+      song = Song.find_by_name(name)\n+      song.update","path":"README.md","position":106,"original_position":106,"commit_id":"f7da55bb7c7ca1ef349f2036ae153045abfffbdf","original_commit_id":"f7da55bb7c7ca1ef349f2036ae153045abfffbdf","user":{"login":"jmburges","id":58342,"avatar_url":"https://avatars.githubusercontent.com/u/58342?v=3","gravatar_id":"","url":"https://api.github.com/users/jmburges","html_url":"https://github.com/jmburges","followers_url":"https://api.github.com/users/jmburges/followers","following_url":"https://api.github.com/users/jmburges/following{/other_user}","gists_url":"https://api.github.com/users/jmburges/gists{/gist_id}","starred_url":"https://api.github.com/users/jmburges/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmburges/subscriptions","organizations_url":"https://api.github.com/users/jmburges/orgs","repos_url":"https://api.github.com/users/jmburges/repos","events_url":"https://api.github.com/users/jmburges/events{/privacy}","received_events_url":"https://api.github.com/users/jmburges/received_events","type":"User","site_admin":false},"body":"Why are you updating the enry here? Also you should be `find_by_id` not `find_by_name`. There may be multiple items with the same name","created_at":"2015-11-18T10:44:16Z","updated_at":"2015-11-18T10:44:16Z","html_url":"https://github.com/learn-co-curriculum/orm-find-or-create-by/pull/1#discussion_r45184435","pull_request_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/pulls/comments/45184435"},"html":{"href":"https://github.com/learn-co-curriculum/orm-find-or-create-by/pull/1#discussion_r45184435"},"pull_request":{"href":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/pulls/1","id":50443968,"html_url":"https://github.com/learn-co-curriculum/orm-find-or-create-by/pull/1","diff_url":"https://github.com/learn-co-curriculum/orm-find-or-create-by/pull/1.diff","patch_url":"https://github.com/learn-co-curriculum/orm-find-or-create-by/pull/1.patch","issue_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/issues/1","number":1,"state":"open","locked":false,"title":"finished readme","user":{"login":"SophieDeBenedetto","id":8999596,"avatar_url":"https://avatars.githubusercontent.com/u/8999596?v=3","gravatar_id":"","url":"https://api.github.com/users/SophieDeBenedetto","html_url":"https://github.com/SophieDeBenedetto","followers_url":"https://api.github.com/users/SophieDeBenedetto/followers","following_url":"https://api.github.com/users/SophieDeBenedetto/following{/other_user}","gists_url":"https://api.github.com/users/SophieDeBenedetto/gists{/gist_id}","starred_url":"https://api.github.com/users/SophieDeBenedetto/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SophieDeBenedetto/subscriptions","organizations_url":"https://api.github.com/users/SophieDeBenedetto/orgs","repos_url":"https://api.github.com/users/SophieDeBenedetto/repos","events_url":"https://api.github.com/users/SophieDeBenedetto/events{/privacy}","received_events_url":"https://api.github.com/users/SophieDeBenedetto/received_events","type":"User","site_admin":false},"body":"needs :eyes:\r\n\r\n@jmburges @AnnJohn ","created_at":"2015-11-11T22:12:40Z","updated_at":"2015-11-18T10:44:16Z","closed_at":null,"merged_at":null,"merge_commit_sha":"02192318e9270c6c39663630d5287849fa725e76","assignee":{"login":"jmburges","id":58342,"avatar_url":"https://avatars.githubusercontent.com/u/58342?v=3","gravatar_id":"","url":"https://api.github.com/users/jmburges","html_url":"https://github.com/jmburges","followers_url":"https://api.github.com/users/jmburges/followers","following_url":"https://api.github.com/users/jmburges/following{/other_user}","gists_url":"https://api.github.com/users/jmburges/gists{/gist_id}","starred_url":"https://api.github.com/users/jmburges/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmburges/subscriptions","organizations_url":"https://api.github.com/users/jmburges/orgs","repos_url":"https://api.github.com/users/jmburges/repos","events_url":"https://api.github.com/users/jmburges/events{/privacy}","received_events_url":"https://api.github.com/users/jmburges/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/pulls/1/commits","review_comments_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/pulls/1/comments","review_comment_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/pulls/comments{/number}","comments_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/issues/1/comments","statuses_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/statuses/f7da55bb7c7ca1ef349f2036ae153045abfffbdf","head":{"label":"learn-co-curriculum:wip-new","ref":"wip-new","sha":"f7da55bb7c7ca1ef349f2036ae153045abfffbdf","user":{"login":"learn-co-curriculum","id":6208017,"avatar_url":"https://avatars.githubusercontent.com/u/6208017?v=3","gravatar_id":"","url":"https://api.github.com/users/learn-co-curriculum","html_url":"https://github.com/learn-co-curriculum","followers_url":"https://api.github.com/users/learn-co-curriculum/followers","following_url":"https://api.github.com/users/learn-co-curriculum/following{/other_user}","gists_url":"https://api.github.com/users/learn-co-curriculum/gists{/gist_id}","starred_url":"https://api.github.com/users/learn-co-curriculum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/learn-co-curriculum/subscriptions","organizations_url":"https://api.github.com/users/learn-co-curriculum/orgs","repos_url":"https://api.github.com/users/learn-co-curriculum/repos","events_url":"https://api.github.com/users/learn-co-curriculum/events{/privacy}","received_events_url":"https://api.github.com/users/learn-co-curriculum/received_events","type":"Organization","site_admin":false},"repo":{"id":46010245,"name":"orm-find-or-create-by","full_name":"learn-co-curriculum/orm-find-or-create-by","owner":{"login":"learn-co-curriculum","id":6208017,"avatar_url":"https://avatars.githubusercontent.com/u/6208017?v=3","gravatar_id":"","url":"https://api.github.com/users/learn-co-curriculum","html_url":"https://github.com/learn-co-curriculum","followers_url":"https://api.github.com/users/learn-co-curriculum/followers","following_url":"https://api.github.com/users/learn-co-curriculum/following{/other_user}","gists_url":"https://api.github.com/users/learn-co-curriculum/gists{/gist_id}","starred_url":"https://api.github.com/users/learn-co-curriculum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/learn-co-curriculum/subscriptions","organizations_url":"https://api.github.com/users/learn-co-curriculum/orgs","repos_url":"https://api.github.com/users/learn-co-curriculum/repos","events_url":"https://api.github.com/users/learn-co-curriculum/events{/privacy}","received_events_url":"https://api.github.com/users/learn-co-curriculum/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/learn-co-curriculum/orm-find-or-create-by","description":"","fork":false,"url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by","forks_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/forks","keys_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/keys{/key_id}","collaborators_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/teams","hooks_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/hooks","issue_events_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/issues/events{/number}","events_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/events","assignees_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/assignees{/user}","branches_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/branches{/branch}","tags_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/tags","blobs_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/git/refs{/sha}","trees_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/git/trees{/sha}","statuses_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/statuses/{sha}","languages_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/languages","stargazers_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/stargazers","contributors_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/contributors","subscribers_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/subscribers","subscription_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/subscription","commits_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/commits{/sha}","git_commits_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/git/commits{/sha}","comments_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/comments{/number}","issue_comment_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/issues/comments{/number}","contents_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/contents/{+path}","compare_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/compare/{base}...{head}","merges_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/merges","archive_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/downloads","issues_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/issues{/number}","pulls_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/pulls{/number}","milestones_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/milestones{/number}","notifications_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/labels{/name}","releases_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/releases{/id}","created_at":"2015-11-11T21:13:58Z","updated_at":"2015-11-11T21:13:58Z","pushed_at":"2015-11-11T22:12:40Z","git_url":"git://github.com/learn-co-curriculum/orm-find-or-create-by.git","ssh_url":"git@github.com:learn-co-curriculum/orm-find-or-create-by.git","clone_url":"https://github.com/learn-co-curriculum/orm-find-or-create-by.git","svn_url":"https://github.com/learn-co-curriculum/orm-find-or-create-by","homepage":null,"size":0,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"learn-co-curriculum:master","ref":"master","sha":"15b4e0e88fb6f1e26824a10054a4f96b0c7972ed","user":{"login":"learn-co-curriculum","id":6208017,"avatar_url":"https://avatars.githubusercontent.com/u/6208017?v=3","gravatar_id":"","url":"https://api.github.com/users/learn-co-curriculum","html_url":"https://github.com/learn-co-curriculum","followers_url":"https://api.github.com/users/learn-co-curriculum/followers","following_url":"https://api.github.com/users/learn-co-curriculum/following{/other_user}","gists_url":"https://api.github.com/users/learn-co-curriculum/gists{/gist_id}","starred_url":"https://api.github.com/users/learn-co-curriculum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/learn-co-curriculum/subscriptions","organizations_url":"https://api.github.com/users/learn-co-curriculum/orgs","repos_url":"https://api.github.com/users/learn-co-curriculum/repos","events_url":"https://api.github.com/users/learn-co-curriculum/events{/privacy}","received_events_url":"https://api.github.com/users/learn-co-curriculum/received_events","type":"Organization","site_admin":false},"repo":{"id":46010245,"name":"orm-find-or-create-by","full_name":"learn-co-curriculum/orm-find-or-create-by","owner":{"login":"learn-co-curriculum","id":6208017,"avatar_url":"https://avatars.githubusercontent.com/u/6208017?v=3","gravatar_id":"","url":"https://api.github.com/users/learn-co-curriculum","html_url":"https://github.com/learn-co-curriculum","followers_url":"https://api.github.com/users/learn-co-curriculum/followers","following_url":"https://api.github.com/users/learn-co-curriculum/following{/other_user}","gists_url":"https://api.github.com/users/learn-co-curriculum/gists{/gist_id}","starred_url":"https://api.github.com/users/learn-co-curriculum/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/learn-co-curriculum/subscriptions","organizations_url":"https://api.github.com/users/learn-co-curriculum/orgs","repos_url":"https://api.github.com/users/learn-co-curriculum/repos","events_url":"https://api.github.com/users/learn-co-curriculum/events{/privacy}","received_events_url":"https://api.github.com/users/learn-co-curriculum/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/learn-co-curriculum/orm-find-or-create-by","description":"","fork":false,"url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by","forks_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/forks","keys_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/keys{/key_id}","collaborators_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/teams","hooks_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/hooks","issue_events_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/issues/events{/number}","events_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/events","assignees_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/assignees{/user}","branches_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/branches{/branch}","tags_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/tags","blobs_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/git/refs{/sha}","trees_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/git/trees{/sha}","statuses_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/statuses/{sha}","languages_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/languages","stargazers_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/stargazers","contributors_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/contributors","subscribers_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/subscribers","subscription_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/subscription","commits_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/commits{/sha}","git_commits_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/git/commits{/sha}","comments_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/comments{/number}","issue_comment_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/issues/comments{/number}","contents_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/contents/{+path}","compare_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/compare/{base}...{head}","merges_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/merges","archive_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/downloads","issues_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/issues{/number}","pulls_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/pulls{/number}","milestones_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/milestones{/number}","notifications_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/labels{/name}","releases_url":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/releases{/id}","created_at":"2015-11-11T21:13:58Z","updated_at":"2015-11-11T21:13:58Z","pushed_at":"2015-11-11T22:12:40Z","git_url":"git://github.com/learn-co-curriculum/orm-find-or-create-by.git","ssh_url":"git@github.com:learn-co-curriculum/orm-find-or-create-by.git","clone_url":"https://github.com/learn-co-curriculum/orm-find-or-create-by.git","svn_url":"https://github.com/learn-co-curriculum/orm-find-or-create-by","homepage":null,"size":0,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/pulls/1"},"html":{"href":"https://github.com/learn-co-curriculum/orm-find-or-create-by/pull/1"},"issue":{"href":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/issues/1"},"comments":{"href":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/learn-co-curriculum/orm-find-or-create-by/statuses/f7da55bb7c7ca1ef349f2036ae153045abfffbdf"}}}}