{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/12753","labels_url":"https://api.github.com/repos/rails/rails/issues/12753/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/12753/comments","events_url":"https://api.github.com/repos/rails/rails/issues/12753/events","html_url":"https://github.com/rails/rails/issues/12753","id":22054515,"number":12753,"title":"Scopes, Sub Selects and Bind Values","user":{"login":"brocktimus","id":2103728,"avatar_url":"https://avatars.githubusercontent.com/u/2103728?v=3","gravatar_id":"","url":"https://api.github.com/users/brocktimus","html_url":"https://github.com/brocktimus","followers_url":"https://api.github.com/users/brocktimus/followers","following_url":"https://api.github.com/users/brocktimus/following{/other_user}","gists_url":"https://api.github.com/users/brocktimus/gists{/gist_id}","starred_url":"https://api.github.com/users/brocktimus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/brocktimus/subscriptions","organizations_url":"https://api.github.com/users/brocktimus/orgs","repos_url":"https://api.github.com/users/brocktimus/repos","events_url":"https://api.github.com/users/brocktimus/events{/privacy}","received_events_url":"https://api.github.com/users/brocktimus/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails/rails/labels/activerecord","name":"activerecord","color":"0b02e1"},{"url":"https://api.github.com/repos/rails/rails/labels/PostgreSQL","name":"PostgreSQL","color":"fbca04"}],"state":"closed","locked":false,"assignee":{"login":"sgrif","id":1529387,"avatar_url":"https://avatars.githubusercontent.com/u/1529387?v=3","gravatar_id":"","url":"https://api.github.com/users/sgrif","html_url":"https://github.com/sgrif","followers_url":"https://api.github.com/users/sgrif/followers","following_url":"https://api.github.com/users/sgrif/following{/other_user}","gists_url":"https://api.github.com/users/sgrif/gists{/gist_id}","starred_url":"https://api.github.com/users/sgrif/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sgrif/subscriptions","organizations_url":"https://api.github.com/users/sgrif/orgs","repos_url":"https://api.github.com/users/sgrif/repos","events_url":"https://api.github.com/users/sgrif/events{/privacy}","received_events_url":"https://api.github.com/users/sgrif/received_events","type":"User","site_admin":false},"milestone":null,"comments":27,"created_at":"2013-11-04T14:34:49Z","updated_at":"2015-02-10T20:48:03Z","closed_at":"2015-02-10T15:57:32Z","body":"This issue is tested using Rails 4.0.1. Running against Postgres 9.3 with the latest pg gem.\r\n\r\nI've only realised recently that AR will change Relations into Sub Selects and have been trying to use them to simplify my queries and scopes.\r\n\r\nI've started to run into a couple of issues, particularly with binds and sub selects. An example of one such problem below:\r\n\r\n```ruby\r\nclass Article < AR::Base\r\n  has_many :comments\r\nend\r\n\r\nclass Comment < AR::Base\r\n  belongs_to\r\n  has_many :replies, class_name: \"Comment\", foreign_key: :parent_id\r\n\r\n  def self.leaves\r\n    where.not(id: all.select(:parent_id))\r\n  end\r\nend\r\n\r\narticle = Article.first\r\nComment.leaves.count # => Works as expected\r\narticle.comments.leaves.count # => Fails with an error due to binds as below\r\n\r\n  PG::ProtocolViolation: ERROR:  bind message supplies 2 parameters, but prepared statement \"a29\" requires 1\r\nSELECT \"comments\".* FROM \"comments\"  WHERE \"comments\".\"article_id\" = $1 AND (\"comments\".\"id\" NOT IN (SELECT parent_id FROM \"comments\"  WHERE \"comments\".\"article_id\" = $1))\r\n```\r\n\r\nI've also got some much more complex examples which have the opposite problem. In those I'm trying to OR together some large and complex scopes. I read that this was possible using ```#where_values``` on a relation. However whenever I try and use that I run into errors because the Arel nodes contain prepared $1 values (and the like) that don't get populated when I do anything with them manually.\r\n\r\nI suspect it could come down to a common problem? I might be misusing something or stretching the ORM too far. I suspect both could be solved if I could \"force\" the substitution of the value into the query selectively. That or manually supply the binds to the query. I've dug about as far as I can get though.\r\n\r\nAny ideas?\r\n\r\nI can provide a more fleshed out example of the larger problem I alluded to. It just needs more models and a bit of time to boil down.\r\n\r\nThanks for the great work guys!"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/73780512","html_url":"https://github.com/rails/rails/issues/12753#issuecomment-73780512","issue_url":"https://api.github.com/repos/rails/rails/issues/12753","id":73780512,"user":{"login":"stiff","id":9119,"avatar_url":"https://avatars.githubusercontent.com/u/9119?v=3","gravatar_id":"","url":"https://api.github.com/users/stiff","html_url":"https://github.com/stiff","followers_url":"https://api.github.com/users/stiff/followers","following_url":"https://api.github.com/users/stiff/following{/other_user}","gists_url":"https://api.github.com/users/stiff/gists{/gist_id}","starred_url":"https://api.github.com/users/stiff/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stiff/subscriptions","organizations_url":"https://api.github.com/users/stiff/orgs","repos_url":"https://api.github.com/users/stiff/repos","events_url":"https://api.github.com/users/stiff/events{/privacy}","received_events_url":"https://api.github.com/users/stiff/received_events","type":"User","site_admin":false},"created_at":"2015-02-10T20:48:03Z","updated_at":"2015-02-10T20:48:03Z","body":"It would be great to add failing test to ensure it's really fixed, because the test I tried ( https://gist.github.com/stiff/4d435d5744ed83d7e6ed ) is working even in 4.1.9 without the error mentioned by @brocktimus."}}