{"action":"created","issue":{"url":"https://api.github.com/repos/activerecord-hackery/squeel/issues/411","repository_url":"https://api.github.com/repos/activerecord-hackery/squeel","labels_url":"https://api.github.com/repos/activerecord-hackery/squeel/issues/411/labels{/name}","comments_url":"https://api.github.com/repos/activerecord-hackery/squeel/issues/411/comments","events_url":"https://api.github.com/repos/activerecord-hackery/squeel/issues/411/events","html_url":"https://github.com/activerecord-hackery/squeel/issues/411","id":124781782,"number":411,"title":"Using complex IN + OR scope inside an association results in invalid SQL on join condition","user":{"login":"azhi","id":752698,"avatar_url":"https://avatars.githubusercontent.com/u/752698?v=3","gravatar_id":"","url":"https://api.github.com/users/azhi","html_url":"https://github.com/azhi","followers_url":"https://api.github.com/users/azhi/followers","following_url":"https://api.github.com/users/azhi/following{/other_user}","gists_url":"https://api.github.com/users/azhi/gists{/gist_id}","starred_url":"https://api.github.com/users/azhi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/azhi/subscriptions","organizations_url":"https://api.github.com/users/azhi/orgs","repos_url":"https://api.github.com/users/azhi/repos","events_url":"https://api.github.com/users/azhi/events{/privacy}","received_events_url":"https://api.github.com/users/azhi/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-01-04T15:43:45Z","updated_at":"2016-02-26T16:43:19Z","closed_at":null,"body":"Squeel 1.2.3 + AR 4.1.14 seems to generate invalid SQL when joining with an association that includes both simple condition and complex IN + OR condition.\r\n\r\nE.g. a `Parent` model with\r\n`has_many :complex_scoped_models, ->{ where(flag: true).where(field: ['test1', 'test2', nil]) }, class_name: 'Model'`\r\nwill generate following SQL when doing `Parent.all.joins(:complex_scoped_models).first`:\r\n\r\n```\r\nSELECT  \"parents\".* FROM \"parents\" INNER JOIN \"models\" ON \"models\".\"parent_id\" = \"parents\".\"id\" AND \"models\".\"flag\" = 't', ((\"models\".\"field\" IN ('test1', 'test2') OR \"models\".\"field\" IS NULL))  ORDER BY \"parents\".\"id\" ASC LIMIT 1\r\n```\r\n\r\nNote the comma after `\"models\".\"flag\" = 't'`, when there should be another AND.\r\nSame scope on the model itself (without an association) seems to be working.\r\n\r\nI've put up a test bench for the issue, you can see full code and test at https://github.com/azhi/squeel_complex_scope_association_test.\r\n\r\nPS: it looks similar to an error pointed by @vellotis in https://github.com/activerecord-hackery/squeel/issues/361#issuecomment-141457924."},"comment":{"url":"https://api.github.com/repos/activerecord-hackery/squeel/issues/comments/189364719","html_url":"https://github.com/activerecord-hackery/squeel/issues/411#issuecomment-189364719","issue_url":"https://api.github.com/repos/activerecord-hackery/squeel/issues/411","id":189364719,"user":{"login":"jlhonora","id":1577721,"avatar_url":"https://avatars.githubusercontent.com/u/1577721?v=3","gravatar_id":"","url":"https://api.github.com/users/jlhonora","html_url":"https://github.com/jlhonora","followers_url":"https://api.github.com/users/jlhonora/followers","following_url":"https://api.github.com/users/jlhonora/following{/other_user}","gists_url":"https://api.github.com/users/jlhonora/gists{/gist_id}","starred_url":"https://api.github.com/users/jlhonora/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jlhonora/subscriptions","organizations_url":"https://api.github.com/users/jlhonora/orgs","repos_url":"https://api.github.com/users/jlhonora/repos","events_url":"https://api.github.com/users/jlhonora/events{/privacy}","received_events_url":"https://api.github.com/users/jlhonora/received_events","type":"User","site_admin":false},"created_at":"2016-02-26T16:43:19Z","updated_at":"2016-02-26T16:43:19Z","body":"Seeing this as well in a similar scenario but with AR 4.2.5. "}}