{"action":"created","issue":{"url":"https://api.github.com/repos/Coursemology/coursemology2/issues/706","labels_url":"https://api.github.com/repos/Coursemology/coursemology2/issues/706/labels{/name}","comments_url":"https://api.github.com/repos/Coursemology/coursemology2/issues/706/comments","events_url":"https://api.github.com/repos/Coursemology/coursemology2/issues/706/events","html_url":"https://github.com/Coursemology/coursemology2/issues/706","id":124327694,"number":706,"title":"cancancan does not correctly use table aliases in the WHERE clause","user":{"login":"kxmbrian","id":6252919,"avatar_url":"https://avatars.githubusercontent.com/u/6252919?v=3","gravatar_id":"","url":"https://api.github.com/users/kxmbrian","html_url":"https://github.com/kxmbrian","followers_url":"https://api.github.com/users/kxmbrian/followers","following_url":"https://api.github.com/users/kxmbrian/following{/other_user}","gists_url":"https://api.github.com/users/kxmbrian/gists{/gist_id}","starred_url":"https://api.github.com/users/kxmbrian/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kxmbrian/subscriptions","organizations_url":"https://api.github.com/users/kxmbrian/orgs","repos_url":"https://api.github.com/users/kxmbrian/repos","events_url":"https://api.github.com/users/kxmbrian/events{/privacy}","received_events_url":"https://api.github.com/users/kxmbrian/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-12-30T09:52:00Z","updated_at":"2016-01-26T12:39:03Z","closed_at":null,"body":"When we define the following rule\r\n```\r\nread_hash = { course_user: { course: { course_users: { user_id: user.id, workflow_state: 'approved', role: [1,2,3] } } } }\r\ncan :read, Course::ExperiencePointsRecord, read_hash\r\n```\r\nCalling `accessible_by` generates the query:\r\n\r\n```sql\r\n...\r\nFROM\r\n\t\"course_experience_points_records\" \r\n\t\tLEFT OUTER JOIN \"course_users\"\r\n\t\t\tON \"course_users\" . \"id\" = \"course_experience_points_records\" . \"course_user_id\" \r\n\t\tLEFT OUTER JOIN \"courses\"\r\n\t\t\tON \"courses\" . \"id\" = \"course_users\" . \"course_id\"\r\n\t\t\t\tAND \"courses\" . \"instance_id\" = $1 \r\n\t\tLEFT OUTER JOIN \"course_users\" \"course_users_courses\"\r\n\t\t\tON \"course_users_courses\" . \"course_id\" = \"courses\" . \"id\"\r\nWHERE\r\n\t\"course_experience_points_records\" . \"course_user_id\" = $2\r\n\tAND (\r\n\t\t\"course_users\" . \"user_id\" = $3\r\n\t\tAND \"course_users\" . \"workflow_state\" = $4\r\n\t\tAND \"course_users\" . \"role\" IN (1, 2, 3)\r\n\t)  [[\"instance_id\", 1], [\"course_user_id\", 4], [\"user_id\", 9], [\"workflow_state\", \"approved\"]]\r\n```\r\nwhich is incorrect.  `course_users` should be generated as `course_users_courses` in the WHERE clause:\r\n```sql\r\nWHERE\r\n\t\"course_experience_points_records\" . \"course_user_id\" = $2\r\n\tAND (\r\n\t\t\"course_users_courses\" . \"user_id\" = $3\r\n\t\tAND \"course_users_courses\" . \"workflow_state\" = $4\r\n\t\tAND \"course_users_courses\" . \"role\" IN (1, 2, 3)\r\n\t)  [[\"instance_id\", 1], [\"course_user_id\", 4], [\"user_id\", 9], [\"workflow_state\", \"approved\"]]\r\n```"},"comment":{"url":"https://api.github.com/repos/Coursemology/coursemology2/issues/comments/174991187","html_url":"https://github.com/Coursemology/coursemology2/issues/706#issuecomment-174991187","issue_url":"https://api.github.com/repos/Coursemology/coursemology2/issues/706","id":174991187,"user":{"login":"lowjoel","id":2505070,"avatar_url":"https://avatars.githubusercontent.com/u/2505070?v=3","gravatar_id":"","url":"https://api.github.com/users/lowjoel","html_url":"https://github.com/lowjoel","followers_url":"https://api.github.com/users/lowjoel/followers","following_url":"https://api.github.com/users/lowjoel/following{/other_user}","gists_url":"https://api.github.com/users/lowjoel/gists{/gist_id}","starred_url":"https://api.github.com/users/lowjoel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lowjoel/subscriptions","organizations_url":"https://api.github.com/users/lowjoel/orgs","repos_url":"https://api.github.com/users/lowjoel/repos","events_url":"https://api.github.com/users/lowjoel/events{/privacy}","received_events_url":"https://api.github.com/users/lowjoel/received_events","type":"User","site_admin":false},"created_at":"2016-01-26T12:39:03Z","updated_at":"2016-01-26T12:39:03Z","body":"Why do we need to worry about repeated tables? If we have two different hashes from two different rules, we just .where(rule_1_hash).where(rule_2_hash) and the resulting query is the conjunction of both?"}}