{"action":"created","comment":{"url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/pulls/comments/70581288","id":70581288,"diff_hunk":"@@ -3,4 +3,123 @@ title:      \"Active Record\"\n ---\n \n \n-Empty for now\n+My mom told me that ActiveRecord doesn't scale!\n+Fortunately, here comes the...\n+\n+# Guidelines for fast & reusable ActiveRecord\n+\n+This is a smell of bad design:\n+\n+> *I need to write a custom SQL query to do this !*\n+>\n+> — No one, ever\n+\n+**This guide** is here to **help you** write ActiveRecord (or any ORM code,\n+really) that lets your app **scale** with good performance and without\n+prematurely optimising the hell out of it.\n+\n+There should be no exceptions to this guide.  If you think you found one, you're\n+probably wrong, but still feel free to issue a pull request and be ready to\n+defend it!\n+\n+## Principles & style\n+\n+Layering and **separation of concerns** are overarching principles here.  There\n+should be a clear separation between your **database** and your **application**:\n+the former is there to persist data, period. When you put something in, it\n+should come out exactly as is, and pulling it out should be reasonably simple. \n+\n+In particular your database should not have any logic: all the (business) rules\n+should be in your application. This means that triggers, stored procedures\n+should be forbidden in general.\n+\n+We make two exceptions to \"logic in the database\":\n+\n+- foreign keys constraints are strongly recommended, provided they're also coded\n+  with Rails, using validations and the appropriate `dependent:` declarations on\n+  associations. <br/>\n+  This is because Rails cannot enforce relationship integrity efficiently.\n+- default values are accepted provided they're also supported by Rails\n+  (automatic in Rails 4+). <br/>\n+  This is because default values are a common requirement and are both a pain\n+  and a source of performance issues if done in Rails.\n+\n+The only case where your database will \"do\" something for you beyond storage is\n+when counting, or otherwise aggregating persisted data. This is essentially for\n+performance reasons.\n+\n+\n+### Controllers, views, presenters, decorators\n+\n+Controllers, views, helpers, view objects, presenters, decorators should never\n+contain any SQL. At all. Even SQL expressions. Seriously, you'll thank us later.\n+\n+The rationale: a class/object that contains SQL\n+- cannot be tested without the database. This makes testing slower and more\n+  brittle.\n+- spreads the responsibility of interacting with the database across multiple\n+  places. We choose to strictly reserve database interaction to query objects,\n+  models, and migrations (more on this below).\n+\n+They should not use relation builders (the `where`, `order`, `group` methods and\n+friends) but instead rely on named scopes; unless the named scopes are trivial.","path":"_guidelines/active-record.md","position":64,"original_position":64,"commit_id":"b7d2df77c4fe8f9eb3ccccb75fb1694e944d370f","original_commit_id":"b7d2df77c4fe8f9eb3ccccb75fb1694e944d370f","user":{"login":"mezis","id":231698,"avatar_url":"https://avatars.githubusercontent.com/u/231698?v=3","gravatar_id":"","url":"https://api.github.com/users/mezis","html_url":"https://github.com/mezis","followers_url":"https://api.github.com/users/mezis/followers","following_url":"https://api.github.com/users/mezis/following{/other_user}","gists_url":"https://api.github.com/users/mezis/gists{/gist_id}","starred_url":"https://api.github.com/users/mezis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mezis/subscriptions","organizations_url":"https://api.github.com/users/mezis/orgs","repos_url":"https://api.github.com/users/mezis/repos","events_url":"https://api.github.com/users/mezis/events{/privacy}","received_events_url":"https://api.github.com/users/mezis/received_events","type":"User","site_admin":false},"body":"> I still think, however, that this should be more like a strong recommendation than an unbreakable rule.\r\n\r\nIf you re-read the meta guideline, you'll see that's exactly what this is ^__^\r\nQuote:\r\n> Of course, guidelines aren’t law — one may ignore them, but there must be a justification, and preferably a comment in the code. “My team doesn’t have time”, or other forms of the threshold of misery, usually aren’t valid reasons to skirt the guidelines :smiling_imp:\r\n\r\n","created_at":"2016-07-13T08:07:28Z","updated_at":"2016-07-13T08:07:28Z","html_url":"https://github.com/deliveroo/deliveroo.engineering/pull/4#discussion_r70581288","pull_request_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/pulls/4","_links":{"self":{"href":"https://api.github.com/repos/deliveroo/deliveroo.engineering/pulls/comments/70581288"},"html":{"href":"https://github.com/deliveroo/deliveroo.engineering/pull/4#discussion_r70581288"},"pull_request":{"href":"https://api.github.com/repos/deliveroo/deliveroo.engineering/pulls/4"}}},"pull_request":{"url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/pulls/4","id":76402719,"html_url":"https://github.com/deliveroo/deliveroo.engineering/pull/4","diff_url":"https://github.com/deliveroo/deliveroo.engineering/pull/4.diff","patch_url":"https://github.com/deliveroo/deliveroo.engineering/pull/4.patch","issue_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/issues/4","number":4,"state":"open","locked":false,"title":"Ruby / ActiveRecord guidelines, part 1","user":{"login":"mezis","id":231698,"avatar_url":"https://avatars.githubusercontent.com/u/231698?v=3","gravatar_id":"","url":"https://api.github.com/users/mezis","html_url":"https://github.com/mezis","followers_url":"https://api.github.com/users/mezis/followers","following_url":"https://api.github.com/users/mezis/following{/other_user}","gists_url":"https://api.github.com/users/mezis/gists{/gist_id}","starred_url":"https://api.github.com/users/mezis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mezis/subscriptions","organizations_url":"https://api.github.com/users/mezis/orgs","repos_url":"https://api.github.com/users/mezis/repos","events_url":"https://api.github.com/users/mezis/events{/privacy}","received_events_url":"https://api.github.com/users/mezis/received_events","type":"User","site_admin":false},"body":"First part of our AR guidelines, ported from the \"old\" ones. Have a read, comment, give the PR a 👍 or 👎 !","created_at":"2016-07-06T09:29:06Z","updated_at":"2016-07-13T08:07:28Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6884ae559c648b15356b521c97559faf9462caff","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/pulls/4/commits","review_comments_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/pulls/4/comments","review_comment_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/pulls/comments{/number}","comments_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/issues/4/comments","statuses_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/statuses/b7d2df77c4fe8f9eb3ccccb75fb1694e944d370f","head":{"label":"deliveroo:ar-guidelines-1","ref":"ar-guidelines-1","sha":"b7d2df77c4fe8f9eb3ccccb75fb1694e944d370f","user":{"login":"deliveroo","id":10514109,"avatar_url":"https://avatars.githubusercontent.com/u/10514109?v=3","gravatar_id":"","url":"https://api.github.com/users/deliveroo","html_url":"https://github.com/deliveroo","followers_url":"https://api.github.com/users/deliveroo/followers","following_url":"https://api.github.com/users/deliveroo/following{/other_user}","gists_url":"https://api.github.com/users/deliveroo/gists{/gist_id}","starred_url":"https://api.github.com/users/deliveroo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/deliveroo/subscriptions","organizations_url":"https://api.github.com/users/deliveroo/orgs","repos_url":"https://api.github.com/users/deliveroo/repos","events_url":"https://api.github.com/users/deliveroo/events{/privacy}","received_events_url":"https://api.github.com/users/deliveroo/received_events","type":"Organization","site_admin":false},"repo":{"id":32532933,"name":"deliveroo.engineering","full_name":"deliveroo/deliveroo.engineering","owner":{"login":"deliveroo","id":10514109,"avatar_url":"https://avatars.githubusercontent.com/u/10514109?v=3","gravatar_id":"","url":"https://api.github.com/users/deliveroo","html_url":"https://github.com/deliveroo","followers_url":"https://api.github.com/users/deliveroo/followers","following_url":"https://api.github.com/users/deliveroo/following{/other_user}","gists_url":"https://api.github.com/users/deliveroo/gists{/gist_id}","starred_url":"https://api.github.com/users/deliveroo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/deliveroo/subscriptions","organizations_url":"https://api.github.com/users/deliveroo/orgs","repos_url":"https://api.github.com/users/deliveroo/repos","events_url":"https://api.github.com/users/deliveroo/events{/privacy}","received_events_url":"https://api.github.com/users/deliveroo/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/deliveroo/deliveroo.engineering","description":"Guidelines and conventions for good software at Deliveroo","fork":true,"url":"https://api.github.com/repos/deliveroo/deliveroo.engineering","forks_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/forks","keys_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/keys{/key_id}","collaborators_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/teams","hooks_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/hooks","issue_events_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/issues/events{/number}","events_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/events","assignees_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/assignees{/user}","branches_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/branches{/branch}","tags_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/tags","blobs_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/git/refs{/sha}","trees_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/git/trees{/sha}","statuses_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/statuses/{sha}","languages_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/languages","stargazers_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/stargazers","contributors_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/contributors","subscribers_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/subscribers","subscription_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/subscription","commits_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/commits{/sha}","git_commits_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/git/commits{/sha}","comments_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/comments{/number}","issue_comment_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/issues/comments{/number}","contents_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/contents/{+path}","compare_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/compare/{base}...{head}","merges_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/merges","archive_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/downloads","issues_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/issues{/number}","pulls_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/pulls{/number}","milestones_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/milestones{/number}","notifications_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/labels{/name}","releases_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/releases{/id}","deployments_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/deployments","created_at":"2015-03-19T16:23:28Z","updated_at":"2016-07-08T13:23:36Z","pushed_at":"2016-07-12T09:01:43Z","git_url":"git://github.com/deliveroo/deliveroo.engineering.git","ssh_url":"git@github.com:deliveroo/deliveroo.engineering.git","clone_url":"https://github.com/deliveroo/deliveroo.engineering.git","svn_url":"https://github.com/deliveroo/deliveroo.engineering","homepage":"","size":656,"stargazers_count":5,"watchers_count":5,"language":"CSS","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":1,"mirror_url":null,"open_issues_count":2,"forks":1,"open_issues":2,"watchers":5,"default_branch":"gh-pages"}},"base":{"label":"deliveroo:gh-pages","ref":"gh-pages","sha":"2c38d1a84e3d97bda4a7340a317bec2cb0bb0e01","user":{"login":"deliveroo","id":10514109,"avatar_url":"https://avatars.githubusercontent.com/u/10514109?v=3","gravatar_id":"","url":"https://api.github.com/users/deliveroo","html_url":"https://github.com/deliveroo","followers_url":"https://api.github.com/users/deliveroo/followers","following_url":"https://api.github.com/users/deliveroo/following{/other_user}","gists_url":"https://api.github.com/users/deliveroo/gists{/gist_id}","starred_url":"https://api.github.com/users/deliveroo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/deliveroo/subscriptions","organizations_url":"https://api.github.com/users/deliveroo/orgs","repos_url":"https://api.github.com/users/deliveroo/repos","events_url":"https://api.github.com/users/deliveroo/events{/privacy}","received_events_url":"https://api.github.com/users/deliveroo/received_events","type":"Organization","site_admin":false},"repo":{"id":32532933,"name":"deliveroo.engineering","full_name":"deliveroo/deliveroo.engineering","owner":{"login":"deliveroo","id":10514109,"avatar_url":"https://avatars.githubusercontent.com/u/10514109?v=3","gravatar_id":"","url":"https://api.github.com/users/deliveroo","html_url":"https://github.com/deliveroo","followers_url":"https://api.github.com/users/deliveroo/followers","following_url":"https://api.github.com/users/deliveroo/following{/other_user}","gists_url":"https://api.github.com/users/deliveroo/gists{/gist_id}","starred_url":"https://api.github.com/users/deliveroo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/deliveroo/subscriptions","organizations_url":"https://api.github.com/users/deliveroo/orgs","repos_url":"https://api.github.com/users/deliveroo/repos","events_url":"https://api.github.com/users/deliveroo/events{/privacy}","received_events_url":"https://api.github.com/users/deliveroo/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/deliveroo/deliveroo.engineering","description":"Guidelines and conventions for good software at Deliveroo","fork":true,"url":"https://api.github.com/repos/deliveroo/deliveroo.engineering","forks_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/forks","keys_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/keys{/key_id}","collaborators_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/teams","hooks_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/hooks","issue_events_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/issues/events{/number}","events_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/events","assignees_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/assignees{/user}","branches_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/branches{/branch}","tags_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/tags","blobs_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/git/refs{/sha}","trees_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/git/trees{/sha}","statuses_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/statuses/{sha}","languages_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/languages","stargazers_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/stargazers","contributors_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/contributors","subscribers_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/subscribers","subscription_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/subscription","commits_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/commits{/sha}","git_commits_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/git/commits{/sha}","comments_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/comments{/number}","issue_comment_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/issues/comments{/number}","contents_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/contents/{+path}","compare_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/compare/{base}...{head}","merges_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/merges","archive_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/downloads","issues_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/issues{/number}","pulls_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/pulls{/number}","milestones_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/milestones{/number}","notifications_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/labels{/name}","releases_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/releases{/id}","deployments_url":"https://api.github.com/repos/deliveroo/deliveroo.engineering/deployments","created_at":"2015-03-19T16:23:28Z","updated_at":"2016-07-08T13:23:36Z","pushed_at":"2016-07-12T09:01:43Z","git_url":"git://github.com/deliveroo/deliveroo.engineering.git","ssh_url":"git@github.com:deliveroo/deliveroo.engineering.git","clone_url":"https://github.com/deliveroo/deliveroo.engineering.git","svn_url":"https://github.com/deliveroo/deliveroo.engineering","homepage":"","size":656,"stargazers_count":5,"watchers_count":5,"language":"CSS","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":1,"mirror_url":null,"open_issues_count":2,"forks":1,"open_issues":2,"watchers":5,"default_branch":"gh-pages"}},"_links":{"self":{"href":"https://api.github.com/repos/deliveroo/deliveroo.engineering/pulls/4"},"html":{"href":"https://github.com/deliveroo/deliveroo.engineering/pull/4"},"issue":{"href":"https://api.github.com/repos/deliveroo/deliveroo.engineering/issues/4"},"comments":{"href":"https://api.github.com/repos/deliveroo/deliveroo.engineering/issues/4/comments"},"review_comments":{"href":"https://api.github.com/repos/deliveroo/deliveroo.engineering/pulls/4/comments"},"review_comment":{"href":"https://api.github.com/repos/deliveroo/deliveroo.engineering/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/deliveroo/deliveroo.engineering/pulls/4/commits"},"statuses":{"href":"https://api.github.com/repos/deliveroo/deliveroo.engineering/statuses/b7d2df77c4fe8f9eb3ccccb75fb1694e944d370f"}}}}
{"actor":{"display_login":"mezis"}}