{"action":"created","comment":{"url":"https://api.github.com/repos/Makeam/AMQ/pulls/comments/32446545","id":32446545,"diff_hunk":"@@ -7,4 +7,13 @@ class Answer < ActiveRecord::Base\n   validates :body, length: { in: 5..3000 }\n   validates :question_id, numericality: true\n \n+  def set_best\n+    self.transaction do\n+      self.question.answers.find_each do |a|","path":"app/models/answer.rb","position":6,"original_position":6,"commit_id":"6fb92029b43a6a5aaf926b8168d8a21b575c01a4","original_commit_id":"6fb92029b43a6a5aaf926b8168d8a21b575c01a4","user":{"login":"palkan","id":1516722,"avatar_url":"https://avatars.githubusercontent.com/u/1516722?v=3","gravatar_id":"","url":"https://api.github.com/users/palkan","html_url":"https://github.com/palkan","followers_url":"https://api.github.com/users/palkan/followers","following_url":"https://api.github.com/users/palkan/following{/other_user}","gists_url":"https://api.github.com/users/palkan/gists{/gist_id}","starred_url":"https://api.github.com/users/palkan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/palkan/subscriptions","organizations_url":"https://api.github.com/users/palkan/orgs","repos_url":"https://api.github.com/users/palkan/repos","events_url":"https://api.github.com/users/palkan/events{/privacy}","received_events_url":"https://api.github.com/users/palkan/received_events","type":"User","site_admin":false},"body":"Вот это уже перебор, а точнее N+1 запрос.\r\nМы находимся в контексте конкретного ответа, и проверять валидность других не нужно.\r\nПоэтому здесь мы обновляем сразу все.\r\n\r\nА вот в следующей строчке как раз надо делать `update!(...)`.\r\n\r\nЭтой транзакцией мы хотим избежать следующей ситуации:\r\n\r\n```ruby\r\n@answer = find_answer\r\n@answer.tеxt = 'ab' # не пройдет валидацию\r\n@answer.set_best # и вот обновление всех ответов произойдет, а данного нет\r\n```","created_at":"2015-06-15T17:44:50Z","updated_at":"2015-06-15T17:44:50Z","html_url":"https://github.com/Makeam/AMQ/pull/5#discussion_r32446545","pull_request_url":"https://api.github.com/repos/Makeam/AMQ/pulls/5","_links":{"self":{"href":"https://api.github.com/repos/Makeam/AMQ/pulls/comments/32446545"},"html":{"href":"https://github.com/Makeam/AMQ/pull/5#discussion_r32446545"},"pull_request":{"href":"https://api.github.com/repos/Makeam/AMQ/pulls/5"}}},"pull_request":{"url":"https://api.github.com/repos/Makeam/AMQ/pulls/5","id":37631629,"html_url":"https://github.com/Makeam/AMQ/pull/5","diff_url":"https://github.com/Makeam/AMQ/pull/5.diff","patch_url":"https://github.com/Makeam/AMQ/pull/5.patch","issue_url":"https://api.github.com/repos/Makeam/AMQ/issues/5","number":5,"state":"open","locked":false,"title":"Lesson 7","user":{"login":"Makeam","id":5992510,"avatar_url":"https://avatars.githubusercontent.com/u/5992510?v=3","gravatar_id":"","url":"https://api.github.com/users/Makeam","html_url":"https://github.com/Makeam","followers_url":"https://api.github.com/users/Makeam/followers","following_url":"https://api.github.com/users/Makeam/following{/other_user}","gists_url":"https://api.github.com/users/Makeam/gists{/gist_id}","starred_url":"https://api.github.com/users/Makeam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Makeam/subscriptions","organizations_url":"https://api.github.com/users/Makeam/orgs","repos_url":"https://api.github.com/users/Makeam/repos","events_url":"https://api.github.com/users/Makeam/events{/privacy}","received_events_url":"https://api.github.com/users/Makeam/received_events","type":"User","site_admin":false},"body":"Вроде все готово. )","created_at":"2015-06-13T21:26:12Z","updated_at":"2015-06-15T17:44:50Z","closed_at":null,"merged_at":null,"merge_commit_sha":"274e5930cfaf7bce5b85d5fae712b9dc21b0044e","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/Makeam/AMQ/pulls/5/commits","review_comments_url":"https://api.github.com/repos/Makeam/AMQ/pulls/5/comments","review_comment_url":"https://api.github.com/repos/Makeam/AMQ/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Makeam/AMQ/issues/5/comments","statuses_url":"https://api.github.com/repos/Makeam/AMQ/statuses/6fb92029b43a6a5aaf926b8168d8a21b575c01a4","head":{"label":"Makeam:les7","ref":"les7","sha":"6fb92029b43a6a5aaf926b8168d8a21b575c01a4","user":{"login":"Makeam","id":5992510,"avatar_url":"https://avatars.githubusercontent.com/u/5992510?v=3","gravatar_id":"","url":"https://api.github.com/users/Makeam","html_url":"https://github.com/Makeam","followers_url":"https://api.github.com/users/Makeam/followers","following_url":"https://api.github.com/users/Makeam/following{/other_user}","gists_url":"https://api.github.com/users/Makeam/gists{/gist_id}","starred_url":"https://api.github.com/users/Makeam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Makeam/subscriptions","organizations_url":"https://api.github.com/users/Makeam/orgs","repos_url":"https://api.github.com/users/Makeam/repos","events_url":"https://api.github.com/users/Makeam/events{/privacy}","received_events_url":"https://api.github.com/users/Makeam/received_events","type":"User","site_admin":false},"repo":{"id":36682006,"name":"AMQ","full_name":"Makeam/AMQ","owner":{"login":"Makeam","id":5992510,"avatar_url":"https://avatars.githubusercontent.com/u/5992510?v=3","gravatar_id":"","url":"https://api.github.com/users/Makeam","html_url":"https://github.com/Makeam","followers_url":"https://api.github.com/users/Makeam/followers","following_url":"https://api.github.com/users/Makeam/following{/other_user}","gists_url":"https://api.github.com/users/Makeam/gists{/gist_id}","starred_url":"https://api.github.com/users/Makeam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Makeam/subscriptions","organizations_url":"https://api.github.com/users/Makeam/orgs","repos_url":"https://api.github.com/users/Makeam/repos","events_url":"https://api.github.com/users/Makeam/events{/privacy}","received_events_url":"https://api.github.com/users/Makeam/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Makeam/AMQ","description":"","fork":false,"url":"https://api.github.com/repos/Makeam/AMQ","forks_url":"https://api.github.com/repos/Makeam/AMQ/forks","keys_url":"https://api.github.com/repos/Makeam/AMQ/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Makeam/AMQ/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Makeam/AMQ/teams","hooks_url":"https://api.github.com/repos/Makeam/AMQ/hooks","issue_events_url":"https://api.github.com/repos/Makeam/AMQ/issues/events{/number}","events_url":"https://api.github.com/repos/Makeam/AMQ/events","assignees_url":"https://api.github.com/repos/Makeam/AMQ/assignees{/user}","branches_url":"https://api.github.com/repos/Makeam/AMQ/branches{/branch}","tags_url":"https://api.github.com/repos/Makeam/AMQ/tags","blobs_url":"https://api.github.com/repos/Makeam/AMQ/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Makeam/AMQ/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Makeam/AMQ/git/refs{/sha}","trees_url":"https://api.github.com/repos/Makeam/AMQ/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Makeam/AMQ/statuses/{sha}","languages_url":"https://api.github.com/repos/Makeam/AMQ/languages","stargazers_url":"https://api.github.com/repos/Makeam/AMQ/stargazers","contributors_url":"https://api.github.com/repos/Makeam/AMQ/contributors","subscribers_url":"https://api.github.com/repos/Makeam/AMQ/subscribers","subscription_url":"https://api.github.com/repos/Makeam/AMQ/subscription","commits_url":"https://api.github.com/repos/Makeam/AMQ/commits{/sha}","git_commits_url":"https://api.github.com/repos/Makeam/AMQ/git/commits{/sha}","comments_url":"https://api.github.com/repos/Makeam/AMQ/comments{/number}","issue_comment_url":"https://api.github.com/repos/Makeam/AMQ/issues/comments{/number}","contents_url":"https://api.github.com/repos/Makeam/AMQ/contents/{+path}","compare_url":"https://api.github.com/repos/Makeam/AMQ/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Makeam/AMQ/merges","archive_url":"https://api.github.com/repos/Makeam/AMQ/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Makeam/AMQ/downloads","issues_url":"https://api.github.com/repos/Makeam/AMQ/issues{/number}","pulls_url":"https://api.github.com/repos/Makeam/AMQ/pulls{/number}","milestones_url":"https://api.github.com/repos/Makeam/AMQ/milestones{/number}","notifications_url":"https://api.github.com/repos/Makeam/AMQ/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Makeam/AMQ/labels{/name}","releases_url":"https://api.github.com/repos/Makeam/AMQ/releases{/id}","created_at":"2015-06-01T19:00:35Z","updated_at":"2015-06-01T20:12:18Z","pushed_at":"2015-06-15T15:38:15Z","git_url":"git://github.com/Makeam/AMQ.git","ssh_url":"git@github.com:Makeam/AMQ.git","clone_url":"https://github.com/Makeam/AMQ.git","svn_url":"https://github.com/Makeam/AMQ","homepage":null,"size":236,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":5,"forks":0,"open_issues":5,"watchers":0,"default_branch":"master"}},"base":{"label":"Makeam:master","ref":"master","sha":"a2ab3f36d8b1592696fc1c9dba5e2be99af28c6c","user":{"login":"Makeam","id":5992510,"avatar_url":"https://avatars.githubusercontent.com/u/5992510?v=3","gravatar_id":"","url":"https://api.github.com/users/Makeam","html_url":"https://github.com/Makeam","followers_url":"https://api.github.com/users/Makeam/followers","following_url":"https://api.github.com/users/Makeam/following{/other_user}","gists_url":"https://api.github.com/users/Makeam/gists{/gist_id}","starred_url":"https://api.github.com/users/Makeam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Makeam/subscriptions","organizations_url":"https://api.github.com/users/Makeam/orgs","repos_url":"https://api.github.com/users/Makeam/repos","events_url":"https://api.github.com/users/Makeam/events{/privacy}","received_events_url":"https://api.github.com/users/Makeam/received_events","type":"User","site_admin":false},"repo":{"id":36682006,"name":"AMQ","full_name":"Makeam/AMQ","owner":{"login":"Makeam","id":5992510,"avatar_url":"https://avatars.githubusercontent.com/u/5992510?v=3","gravatar_id":"","url":"https://api.github.com/users/Makeam","html_url":"https://github.com/Makeam","followers_url":"https://api.github.com/users/Makeam/followers","following_url":"https://api.github.com/users/Makeam/following{/other_user}","gists_url":"https://api.github.com/users/Makeam/gists{/gist_id}","starred_url":"https://api.github.com/users/Makeam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Makeam/subscriptions","organizations_url":"https://api.github.com/users/Makeam/orgs","repos_url":"https://api.github.com/users/Makeam/repos","events_url":"https://api.github.com/users/Makeam/events{/privacy}","received_events_url":"https://api.github.com/users/Makeam/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Makeam/AMQ","description":"","fork":false,"url":"https://api.github.com/repos/Makeam/AMQ","forks_url":"https://api.github.com/repos/Makeam/AMQ/forks","keys_url":"https://api.github.com/repos/Makeam/AMQ/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Makeam/AMQ/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Makeam/AMQ/teams","hooks_url":"https://api.github.com/repos/Makeam/AMQ/hooks","issue_events_url":"https://api.github.com/repos/Makeam/AMQ/issues/events{/number}","events_url":"https://api.github.com/repos/Makeam/AMQ/events","assignees_url":"https://api.github.com/repos/Makeam/AMQ/assignees{/user}","branches_url":"https://api.github.com/repos/Makeam/AMQ/branches{/branch}","tags_url":"https://api.github.com/repos/Makeam/AMQ/tags","blobs_url":"https://api.github.com/repos/Makeam/AMQ/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Makeam/AMQ/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Makeam/AMQ/git/refs{/sha}","trees_url":"https://api.github.com/repos/Makeam/AMQ/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Makeam/AMQ/statuses/{sha}","languages_url":"https://api.github.com/repos/Makeam/AMQ/languages","stargazers_url":"https://api.github.com/repos/Makeam/AMQ/stargazers","contributors_url":"https://api.github.com/repos/Makeam/AMQ/contributors","subscribers_url":"https://api.github.com/repos/Makeam/AMQ/subscribers","subscription_url":"https://api.github.com/repos/Makeam/AMQ/subscription","commits_url":"https://api.github.com/repos/Makeam/AMQ/commits{/sha}","git_commits_url":"https://api.github.com/repos/Makeam/AMQ/git/commits{/sha}","comments_url":"https://api.github.com/repos/Makeam/AMQ/comments{/number}","issue_comment_url":"https://api.github.com/repos/Makeam/AMQ/issues/comments{/number}","contents_url":"https://api.github.com/repos/Makeam/AMQ/contents/{+path}","compare_url":"https://api.github.com/repos/Makeam/AMQ/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Makeam/AMQ/merges","archive_url":"https://api.github.com/repos/Makeam/AMQ/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Makeam/AMQ/downloads","issues_url":"https://api.github.com/repos/Makeam/AMQ/issues{/number}","pulls_url":"https://api.github.com/repos/Makeam/AMQ/pulls{/number}","milestones_url":"https://api.github.com/repos/Makeam/AMQ/milestones{/number}","notifications_url":"https://api.github.com/repos/Makeam/AMQ/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Makeam/AMQ/labels{/name}","releases_url":"https://api.github.com/repos/Makeam/AMQ/releases{/id}","created_at":"2015-06-01T19:00:35Z","updated_at":"2015-06-01T20:12:18Z","pushed_at":"2015-06-15T15:38:15Z","git_url":"git://github.com/Makeam/AMQ.git","ssh_url":"git@github.com:Makeam/AMQ.git","clone_url":"https://github.com/Makeam/AMQ.git","svn_url":"https://github.com/Makeam/AMQ","homepage":null,"size":236,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":5,"forks":0,"open_issues":5,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/Makeam/AMQ/pulls/5"},"html":{"href":"https://github.com/Makeam/AMQ/pull/5"},"issue":{"href":"https://api.github.com/repos/Makeam/AMQ/issues/5"},"comments":{"href":"https://api.github.com/repos/Makeam/AMQ/issues/5/comments"},"review_comments":{"href":"https://api.github.com/repos/Makeam/AMQ/pulls/5/comments"},"review_comment":{"href":"https://api.github.com/repos/Makeam/AMQ/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Makeam/AMQ/pulls/5/commits"},"statuses":{"href":"https://api.github.com/repos/Makeam/AMQ/statuses/6fb92029b43a6a5aaf926b8168d8a21b575c01a4"}}}}